1

我在 VBA 中有以下两个函数。我的excel看起来像这样。

OddsId  Agt      Ma   Sma     Result
1       Agt1    Ma1   Sma1     x

我的意图是计算 x 的值。我想添加一个条件,例如,用户一次只能在 Agt、ma 和 Sma 中提供一个值。如果这两个字段中的任何一个不为零,那么我的结果应该是“错误”。我不确定在这些函数中的何处添加此条件。

我也不太确定 If 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
4

1 回答 1

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
于 2013-07-07T10:47:42.890 回答