1

我希望有人能回答这个问题。我不认为这是可能的,但我想要做的是使用字符串作为标准来设置一个 if 函数。例如:

With ThisWorkbook.Worksheets("Sheet1")
    stNumerator = .Cells(6, 3).Value
    stDenominator = .Cells(6, 4).Value

    If .Cells(7, 3) = "x" Then
        stCriteria = stNumerator & " >0"
    End If

    If .Cells(8, 4) = "x" Then
        stCriteria = stCriteria & " and " & stDenominator & " <>0"
    End If

    If stCriteria Then
         .Cells(1, 1) = stNumerator / stDenominator
    End If 

所以stCriteria应该等于stNumerator > 0 and stDenominator <>0

4

1 回答 1

1

你想要的可以使用来实现Application.Evaluate

这是一个例子

Sub Sample()
    Dim stCriteria1 As String, stCriteria2 As String

    With ThisWorkbook.Worksheets("Sheet1")            
        stNumerator = .Cells(6, 3).Value
        stDenominator = .Cells(6, 4).Value

        If .Cells(7, 3) = "x" Then stCriteria1 = stNumerator & " >0"
        If .Cells(8, 4) = "x" Then stCriteria2 = stDenominator & " <>0"

        If stCriteria1 <> "" And stCriteria2 <> "" Then
            If Application.Evaluate(stCriteria1) And _
            Application.Evaluate(stCriteria2) Then
                 .Cells(1, 1) = stNumerator / stDenominator
            End If
        ElseIf stCriteria1 <> "" Then
            If Application.Evaluate(stCriteria1) Then
                 .Cells(1, 1) = stNumerator / stDenominator
            End If
        ElseIf stCriteria2 <> "" Then
            If Application.Evaluate(stCriteria2) Then
                 .Cells(1, 1) = stNumerator / stDenominator
            End If
        End If
    End With
End Sub
于 2013-11-14T09:43:08.850 回答