Vba 限制到其他条件?
因此,我有一些VBA代码,设计用于根据单元格中的值进行一些操作。读取单元格中的值,并相应地运行代码。这是毫无疑问的Vba 限制到其他条件?,vba,excel,if-statement,Vba,Excel,If Statement,因此,我有一些VBA代码,设计用于根据单元格中的值进行一些操作。读取单元格中的值,并相应地运行代码。这是毫无疑问的 If Target.Value = "something" Then End If ElseIf Target.Value = "something2" Then End If ElseIf Target.Value = "something3" Then End If ElseIf Target.Value = "something4" Then End If 这很好,但是如果
If Target.Value = "something" Then
End If
ElseIf Target.Value = "something2" Then
End If
ElseIf Target.Value = "something3" Then
End If
ElseIf Target.Value = "something4" Then
End If
这很好,但是如果我添加一个额外的ElseIf条件,我会得到没有if的Else的编译器错误。在第五个条件中将ElseIf更改为Else并不能解决问题。我的问题是,我可以运行的ElseIf条件数量是否有限制?事实上,我只需要第五个就可以完全完成我要做的事情。我确信条件中的代码没有错误。如果只在末尾执行,则结束:
If Target.Value = "something" Then
'Do something
ElseIf Target.Value = "something2" Then
'Do something
ElseIf Target.Value = "something3" Then
'Do something
ElseIf Target.Value = "something4" Then
'Do something
End If
如果仅在结尾处结束,则结束:
If Target.Value = "something" Then
'Do something
ElseIf Target.Value = "something2" Then
'Do something
ElseIf Target.Value = "something3" Then
'Do something
ElseIf Target.Value = "something4" Then
'Do something
End If
当然,你应该升级你的多个ElseIf并选择案例 这将使您在将来更轻松地实现更多场景
Select Case Target.Value
Case "something"
' code 1 here
Case "something2"
' code 2 here
Case "something3"
' code 3 here
Case "something4"
' code 4 here
Case "something5", "something6" ' <-- replaces using another If with And
' code 5 here
' Case ....
End Select
当然,你应该升级你的多个ElseIf并选择案例 这将使您在将来更轻松地实现更多场景
Select Case Target.Value
Case "something"
' code 1 here
Case "something2"
' code 2 here
Case "something3"
' code 3 here
Case "something4"
' code 4 here
Case "something5", "something6" ' <-- replaces using another If with And
' code 5 here
' Case ....
End Select
这没有问题什么?当然???啊,就是这样。。。代码有点凌乱,所以我假设最后一个ElseIf语句的EndIf在所有其他语句中,但事实并非如此。我的错误应该被发现了。我能运行的ElseIf条件数量有限制吗?不过,在10K行之后,nope模块将停止编译。限制是你能在精神上跟踪多少。在一个点上,你应该考虑一个选择的case块,如果可能的话,你会考虑每种情况都有一个单独的过程,也许是一个CalbByNAMEY或Apple应用程序。运行一个运行适当的方法的调用——然后你可以有一个字典,用目标值。运行strategiesTarget.Value,使其成为一个1行程序!这没有问题什么?当然???啊,就是这样。。。代码有点凌乱,所以我假设最后一个ElseIf语句的EndIf在所有其他语句中,但事实并非如此。我的错误应该被发现了。我能运行的ElseIf条件数量有限制吗?不过,在10K行之后,nope模块将停止编译。限制是你能在精神上跟踪多少。在一个点上,你应该考虑一个选择的case块,如果可能的话,你会考虑每种情况都有一个单独的过程,也许是一个CalbByNAMEY或Apple应用程序。运行一个运行适当的方法的调用——然后你可以有一个字典,用目标值。运行strategiesTarget.Value,使其成为一个1行程序!是的,我现在明白我的错误了,哈哈。还是新手,谢谢你的回复!是的,我现在明白我的错误了,哈哈。还是新手,谢谢你的回复!好的,我来看看!谢谢你的回复@姜饼PK一旦你尝试了精选案例,你就再也回不去了:好吧,我来看看!谢谢你的回复@GingerbreadPK一旦您尝试Select Case,您将永远不会回头: