2013-07-07 88 views
1

我在VBA中有以下兩個函數。我的excel看起來就像這樣。在循環中添加條件

OddsId Agt  Ma Sma  Result 
1  Agt1 Ma1 Sma1  x 

我的意圖是計算x的值。我想添加一個條件,用戶可以在Agt,ma和Sma中一次只提供一個值。如果這兩個字段中的任何一個都不是零,那麼我的結果應該是「Error」。我不確定在這些功能中添加這個條件的位置。

我也不是很確定如果NOT函數實際上會返回什麼! 請幫助我。

Function fcComm1(oddsId As Integer, agt As String, ma As String, sma As String, fcOption1 As Integer) 
If Not setFcType(agt, ma, sma) Then 
    fcComm1 = "Invalid2" 
    Return 
End If 
For i = 1 To bets.ListRows.Count 
    currOddsId = bets.ListColumns("OddsId").DataBodyRange(i) 
    currTransId = bets.ListColumns("TransId").DataBodyRange(i) 
    currPlayer = bets.ListColumns("Account").DataBodyRange(i) 

    If (currOddsId = oddsId) Then 
     If FcType = "agt" Then 
      currAgt = Application.WorksheetFunction.VLookup(currPlayer, accounts.DataBodyRange, 9, False) 
      If (agt <> currAgt) Then 
       fcComm1 = "" 
       GoTo NextIteration 
      End If 
     ElseIf FcType = "ma" Then 
      currMa = Application.WorksheetFunction.VLookup(currPlayer, accounts.DataBodyRange, 10, False) 
      If (ma <> currMa) Then 
      fcComm1 = "" 
       GoTo NextIteration 
      End If 
     ElseIf FcType = "sma" Then 
      currSma = Application.WorksheetFunction.VLookup(currPlayer, accounts.DataBodyRange, 11, False) 
      If (sma <> currSma) Then 
      fcComm1 = "" 
       GoTo NextIteration 
      End If 
     End If 


     exchangeRate = Application.WorksheetFunction.VLookup(currPlayer, accounts.DataBodyRange, 4, False) 
     blindRisk = Application.WorksheetFunction.VLookup(currPlayer, accounts.DataBodyRange, 6, False)/100# 
     fcComm1 = fcComm1 + ForecastBet(currTransId, exchangeRate, blindRisk) 

NextIteration: 

    End If 
Next 
End Function 



Function setFcType(agt As String, ma As String, sma As String) 
setFcType = True 

If Len(agt) + Len(ma) + Len(sma) = 0 Then 
    FcType = "company" 
ElseIf agt <> "" And agt <> "0" Then 
    FcType = "agt" 
ElseIf ma <> "" And ma <> "0" Then 
    FcType = "ma" 
ElseIf sma <> "" And sma <> "0" Then 
    FcType = "sma" 
End If 

End Function 
+0

注意:所有的變量都是正確定義的,儘管我在這裏粘貼代碼的時候刪除了這些變量。目前這段代碼工作正常。我肯定需要添加一些條件。 Thnks。 – Shan

回答

0

這裏,你有你的setFcType功能比「」不止一個變量不同的情況下返回false的修正版本。

Function setFcType(agt As String, ma As String, sma As String) 
setFcType = True 

If ((Len(Trim(agt)) > 0 And (Len(Trim(ma)) > 0 Or Len(Trim(sma)) > 0)) Or (Len(Trim(ma)) > 0 And (Len(Trim(agt)) > 0 Or Len(Trim(sma)) > 0))) Then 
    setFcType = False 
    Exit Function 
End If 

If Len(agt) + Len(ma) + Len(sma) = 0 Then 
    FcType = "company" 
ElseIf agt <> "" And agt <> "0" Then 
    FcType = "agt" 
ElseIf ma <> "" And ma <> "0" Then 
    FcType = "ma" 
ElseIf sma <> "" And sma <> "0" Then 
    FcType = "sma" 
End If 

End Function 
+0

非常感謝@varocarbas。它幫助我解決了這個問題。 – Shan

+0

不客氣。你的描述和代碼是清晰的(事實並非總是如此......)。 – varocarbas