Excel VBA If语句多条件运行时错误13



你好,每当我尝试运行以下代码时,我都会遇到运行时错误13类型不匹配,我不知道为什么或如何修复。

这个想法是,如果a=8,单元格(i,c(中的第一个数字=6或4,那么excel应该是这样的。

Dim a as integer
Dim c as integer
If a = 8 And (CInt(Left(.Cells(i, c), 1)) = 6 Or CInt(Left(.Cells(i, c), 1)) = 4) Then```

如果正在读取的单元格的内容为空或不是以数字开头,CInt将抛出运行时错误13。

您可以使用Val-函数(对于非数字字符串将返回0(,也可以检查字符"6"resp。CCD_ 4。我还建议使用一个中间变量来保存您正在检查的字符,使代码可读性更强。

dim firstChr as string
firstChr = Left(.Cells(i, c))
If a = 8 And (val(firstChr) = 6 Or val(firstChr) = 4) Then
' or
If a = 8 And (firstChr = "6" Or firstChr = "4") Then

最新更新