Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在VBA中使用字符串表示if函数的条件_String_Vba_Excel_If Statement - Fatal编程技术网

String 在VBA中使用字符串表示if函数的条件

String 在VBA中使用字符串表示if函数的条件,string,vba,excel,if-statement,String,Vba,Excel,If Statement,我希望有人能回答这个问题。我认为这是不可能的,但我正在尝试使用字符串作为标准设置一个if函数。例如: With ThisWorkbook.Worksheets("Sheet1") stNumerator = .Cells(6, 3).Value stDenominator = .Cells(6, 4).Value If .Cells(7, 3) = "x" Then stCriteria = stNumerator & " >0"

我希望有人能回答这个问题。我认为这是不可能的,但我正在尝试使用字符串作为标准设置一个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 
使用此工作簿。工作表(“表1”)
stNumerator=.Cells(6,3).Value
stDenominator=.Cells(6,4).Value
如果.Cells(7,3)=“x”,则
stCriteria=stNumerator&>0
如果结束
如果.Cells(8,4)=“x”,则
stCriteria=stCriteria&“和”&stDenominator&“0”
如果结束
如果是,那么
.单元格(1,1)=标准分子/标准分母
如果结束

所以
stCriteria
应该等于
stNumerator>0和stDenominator 0
使用
应用程序可以实现您想要的。评估

这里有一个例子

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
子样本()
将标准1设置为字符串,标准2设置为字符串
使用此工作簿。工作表(“表1”)
stNumerator=.Cells(6,3).Value
stDenominator=.Cells(6,4).Value
如果.Cells(7,3)=“x”,则标准1=stNumerator&>0
如果.Cells(8,4)=“x”,则标准2=stDenominator&“0”
如果标准1“”和标准2“”,则
如果应用。评估(标准1)和_
应用。评估(标准2)然后
.单元格(1,1)=标准分子/标准分母
如果结束
如果标准1“那么
如果应用程序。评估(标准1),则
.单元格(1,1)=标准分子/标准分母
如果结束
其他标准2“那么
如果应用程序。评估(标准2),则
.单元格(1,1)=标准分子/标准分母
如果结束
如果结束
以
端接头

使用
应用程序可以实现您想要的。评估

这里有一个例子

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
子样本()
将标准1设置为字符串,标准2设置为字符串
使用此工作簿。工作表(“表1”)
stNumerator=.Cells(6,3).Value
stDenominator=.Cells(6,4).Value
如果.Cells(7,3)=“x”,则标准1=stNumerator&>0
如果.Cells(8,4)=“x”,则标准2=stDenominator&“0”
如果标准1“”和标准2“”,则
如果应用。评估(标准1)和_
应用。评估(标准2)然后
.单元格(1,1)=标准分子/标准分母
如果结束
如果标准1“那么
如果应用程序。评估(标准1),则
.单元格(1,1)=标准分子/标准分母
如果结束
其他标准2“那么
如果应用程序。评估(标准2),则
.单元格(1,1)=标准分子/标准分母
如果结束
如果结束
以
端接头

在谷歌上搜索这个问题太难了——你得到的只是关于字符串比较的线索。谢谢。在谷歌上搜索这个问题太难了——你得到的只是关于字符串比较的线索。谢谢你。