標題:
[發問]
請問,Function中,由固定儲存格改為變數儲存格問題?
[打印本頁]
作者:
yoya
時間:
2014-6-11 15:25
標題:
請問,Function中,由固定儲存格改為變數儲存格問題?
Public Function GetOrderText()
If Sheet1.Cells(1, 2) = "S" Then
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)
End If
End Function
複製代碼
以上是原程式編碼,而我想把其中幾項改為變數,依造儲存格不同而跑不同的代號
以下是我改寫的部分
Public Function GetOrderText(OrderStock As String,OrderPrice As String)
If Sheet1.Cells(1, 2) = "S" Then
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)
End If
End Function
複製代碼
如果用原範本,電腦輸出到程式後系統可以判別這個下單字串,
但是當我把它改為變數之後,系統就讀不到我的下單字串。
請問是我在把他改為變數的過程中,有遺漏什麼嗎??亦或是設定錯誤??
因為我接觸VBA才一個禮拜,都是從錯誤中學習,希望各位先進能不吝指導!!!
謝謝各位先進了!!!
作者:
GBKEE
時間:
2014-6-12 07:06
回復
1#
yoya
但是當我把它改為變數之後,系統就讀不到我的下單字串。
可以啊,
Option Explicit
Sub Ex()
Dim A As String, B As Boolean
'程式中使用函數 有參數給參數,沒參數不用給
Sheet1.Cells(1, 2) = "S"
A = GetOrderText("第一個參數OrderStock的字串", "第二個參數OrderPrice的字串")
B = Test_Function
MsgBox A
MsgBox B
Sheet1.Cells(1, 2) = "s"
MsgBox GetOrderText("第一個參數OrderStock的字串", "第二個參數OrderPrice的字串")
MsgBox Test_Function
End Sub
Function Test_Function() As Boolean
If Sheet1.Cells(1, 2) = "S" Then Test_Function = True
End Function
Public Function GetOrderText(OrderStock As String, OrderPrice As String)
GetOrderText = "GetOrderText ???"
If Sheet1.Cells(1, 2) = "S" Then
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)
End If
End Function
複製代碼
作者:
yoya
時間:
2014-6-12 09:15
嗯嗯~~~這樣可能是我在設計整個程式的時候~~邏輯上出了問題~~
非常感謝謝版主的回覆唷!!!!!
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)