简体   繁体   English

如果 LEN(Sheet1.Range("A1") > 0) 返回 0,但仍在运行代码?

[英]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.

相关问题 如何匹配/搜索excel VBA中Sheet2.Range(“A1:A10”)中的Sheet1.Range(“A1”)的值 - How to match/search the value of Sheet1.Range(“A1”) in Sheet2.Range(“A1:A10”) in excel VBA 使用ThisWorkbook.Sheets(1)的Excel vba代码.Range不工作,但Sheet1.Range工作正常。 为什么? - Excel vba code using ThisWorkbook.Sheets(1).Range not working, but Sheet1.Range works fine. Why? 找到了'TargetTable.Range.SpecialCells(xlCellTypeVisible).Copy _'目标:= Sheets(“ Sheet8”)。Range(“ A1”) - found 'TargetTable.Range.SpecialCells(xlCellTypeVisible).Copy _ ' Destination:=Sheets(“Sheet8”).Range(“A1”) 转置'Sheet1'!A1与B1!A1 - Transpose 'Sheet1'!A1 with B1!A1 Sheets(“sheet_1”).ActiveCell.Offset(0, i + 1).Range(“A1”).Value 的正确语法是什么 - What is the correct syntax for Sheets(“sheet_1”).ActiveCell.Offset(0, i + 1).Range(“A1”).Value 单个单元格的范围地址(A1)有时返回单元格范围地址“A1:A1”而不是“A1” - Range Address for Single Cell (A1) sometimes returns Cell Range Adress “A1:A1” instead of “A1” 将工作表1中的行A1复制到工作表2中的行A1 - Copy Row A1 from Sheet 1 into Row A1 Sheet 2 具有Range(“ A1:B3”)的简单VBA代码。排序不适用于包含公式的单元格 - Simple VBA code with Range(“A1:B3”).Sort does not work with cells including formulas 当Sheet1 A1:A2更改时,自动将Sheet 1 A1:A2复制到Sheet 2 A1:A2 - Automatically Copy Sheet 1 A1:A2 to Sheet 2 A1:A2 When Sheet1 A1:A2 Changes 粘贴\\返回到打开的Excel工作表的单元格A1 - Paste\Return into Cell A1 of open Excel sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM