VBA if语句的类型不匹配



我有一个if语句,用于检查强制转换为双精度的变量的相等性。它检查变量是否包含任何内容,以及数字是否大于0。

然而,当我检查变量(num1(=0还是num1="0"时&";,我收到一个类型不匹配错误。

num1是从工作表中提取的数字,作为双精度:

num1 = loc.Cells(6, 5).Value 

这当前将7800的值分配给num1:num1=7800

然而,ElseIf语句上的代码错误表明存在类型不匹配:

ElseIf num1 = 0 Or num1 = "" Then
GoTo nocurrentnumber
If Not makenumber Then
makenumber = NumberMaker(startdate, enddate)
Set numbersheet = ThisWorkbook.Worksheets("Current Number")
Else
If PnL_Sheet.Cells(Startrow, i).Value > StartDate And PnL_Sheet.Cells(Startrow, i).Value <= EndDate Then
PnL_Sheet.Cells(Startrow, numberLine).Value = number
Else
End If
End If
nocurrentnumber:
Else
PnL_Sheet.Cells(Startrow, numberLine).Value = 0

难道设置为双倍的变量不应该能够检查这些类型的相等性吗?还是我错过了什么?

我读过以下帖子,每一篇都提供了一个我认为无法使用的答案:

Excel VBA运行时错误';13';:类型不匹配

Excel VBA运行时错误';13';类型不匹配

Excel VBA:类型不匹配

双数据类型不能容纳字符串,因此= ""不正确。

VBA中为空的数字数据类型(int、double、long等(将包含值0

ElseIf num1 = 0 then

删除字符串比较。

参考:

https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/double-data-type
Double的默认值为0。

答案不是检查num1是否为空,而是检查num1的len是否大于0。

最新更新