[英]If LEN(Sheet1.Range("A1") > 0) Returning 0, But Still Running Code?
I have an If ElseIf statement that is not working as intended.我有一个没有按预期工作的 If ElseIf 语句。 Below is a sample of the code that I am working with:下面是我正在使用的代码示例:
If LEN(Sheet1.Range("A1") > 0) Then
Do something
ElseIf LEN(Sheet1.Range("A2") > 0) Then
Do something
The problem is that even when the length of A1 is 0 and the length of A2 is greater than 0 (which I have tested many times), the first If statement runs and completely ignores the ElseIf statement.问题是即使 A1 的长度为 0 且 A2 的长度大于 0(我已经测试过很多次了),第一个 If 语句运行并完全忽略了 ElseIf 语句。 I also set up a Debug.Print(LEN(Sheet1.Range("A1"))) statement under the first If statement and that also returns 0 in the console.我还在第一个 If 语句下设置了 Debug.Print(LEN(Sheet1.Range("A1"))) 语句,并且在控制台中也返回 0。 I am not quite sure where the error is.我不太确定错误在哪里。
Any help is appreciated.任何帮助表示赞赏。 Thank you谢谢
Do the following:请执行下列操作:
Sub Button_Click()
If Len(Sheet1.Range("A1")) > 0 Then MsgBox "A1"
If Len(Sheet1.Range("A2")) > 0 Then MsgBox "A2"
End Sub
The comparison must be outside the Len
function, and you should not use elseif
in this scenario.比较必须在Len
function 之外,在这种情况下您不应使用elseif
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.