Board logo

標題: [發問] 請問,Function中,由固定儲存格改為變數儲存格問題? [打印本頁]

作者: yoya    時間: 2014-6-11 15:25     標題: 請問,Function中,由固定儲存格改為變數儲存格問題?

  1. Public Function GetOrderText()
  2.   If Sheet1.Cells(1, 2) = "S" Then
  3.      GetOrderText = "Account=" & Sheet1.Cells(3, 2) & ",ProductID=" & Sheet1.Cells(5, 2) & ",TradeType=" & Sheet1.Cells(6, 2) & ", TradeKind=" & Sheet1.Cells(7, 2) & ", OrderType=" & Sheet1.Cells(8, 2) & ", BuySell=" & Sheet1.Cells(9, 2) & ", Qty=" & Sheet1.Cells(10, 2) & ", Price=" & Sheet1.Cells(11, 2) & ", OrderNo=" & Sheet1.Cells(12, 2) & ", TradeDate=" & Sheet1.Cells(13, 2) & ", BasketNo=" & Sheet1.Cells(14, 2)
  4. End If
  5. End Function
複製代碼
以上是原程式編碼,而我想把其中幾項改為變數,依造儲存格不同而跑不同的代號

以下是我改寫的部分
  1. Public Function GetOrderText(OrderStock As String,OrderPrice As String)
  2.   If Sheet1.Cells(1, 2) = "S" Then
  3.      GetOrderText = "Account=" & Sheet1.Cells(3, 2) & ",ProductID=" & OrderStock & ",TradeType=" & Sheet1.Cells(6, 2) & ", TradeKind=" & Sheet1.Cells(7, 2) & ", OrderType=" & Sheet1.Cells(8, 2) & ", BuySell=" & Sheet1.Cells(9, 2) & ", Qty=" & Sheet1.Cells(10, 2) & ", Price=" & OrderPrice & ", OrderNo=" & Sheet1.Cells(12, 2) & ", TradeDate=" & Sheet1.Cells(13, 2) & ", BasketNo=" & Sheet1.Cells(14, 2)
  4. End If
  5. End Function
複製代碼
如果用原範本,電腦輸出到程式後系統可以判別這個下單字串,
但是當我把它改為變數之後,系統就讀不到我的下單字串。

請問是我在把他改為變數的過程中,有遺漏什麼嗎??亦或是設定錯誤??
因為我接觸VBA才一個禮拜,都是從錯誤中學習,希望各位先進能不吝指導!!!
謝謝各位先進了!!!
作者: GBKEE    時間: 2014-6-12 07:06

回復 1# yoya
但是當我把它改為變數之後,系統就讀不到我的下單字串。

可以啊,
  1. Option Explicit
  2. Sub Ex()
  3.     Dim A As String, B As Boolean
  4.     '程式中使用函數 有參數給參數,沒參數不用給
  5.      Sheet1.Cells(1, 2) = "S"
  6.     A = GetOrderText("第一個參數OrderStock的字串", "第二個參數OrderPrice的字串")
  7.     B = Test_Function
  8.     MsgBox A
  9.     MsgBox B
  10.     Sheet1.Cells(1, 2) = "s"
  11.     MsgBox GetOrderText("第一個參數OrderStock的字串", "第二個參數OrderPrice的字串")
  12.     MsgBox Test_Function
  13. End Sub
  14. Function Test_Function() As Boolean
  15.     If Sheet1.Cells(1, 2) = "S" Then Test_Function = True
  16. End Function

  17. Public Function GetOrderText(OrderStock As String, OrderPrice As String)
  18.     GetOrderText = "GetOrderText ???"
  19.     If Sheet1.Cells(1, 2) = "S" Then
  20.         GetOrderText = "Account=" & Sheet1.Cells(3, 2) & ",ProductID=" & OrderStock & ",TradeType=" & Sheet1.Cells(6, 2) & ", TradeKind=" & Sheet1.Cells(7, 2) & ", OrderType=" & Sheet1.Cells(8, 2) & ", BuySell=" & Sheet1.Cells(9, 2) & ", Qty=" & Sheet1.Cells(10, 2) & ", Price=" & OrderPrice & ", OrderNo=" & Sheet1.Cells(12, 2) & ", TradeDate=" & Sheet1.Cells(13, 2) & ", BasketNo=" & Sheet1.Cells(14, 2)
  21.     End If
  22. End Function
複製代碼

作者: yoya    時間: 2014-6-12 09:15

嗯嗯~~~這樣可能是我在設計整個程式的時候~~邏輯上出了問題~~
非常感謝謝版主的回覆唷!!!!!




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)