不在VBA中
If Sheets("DealComparison").Cells(z + 1, 1) <> "" Then
这个说法是对的吗? 我想检查VBA中的特定工作表是否不等于NULL
Dim x As Integer x = 2 Dim z As Long Dim Myarray(1 To 9) As String For z = 1 To 9 If Trim(Sheets("DealComparison").Cells(z + 1, 1)) <> "" Then Myarray(z) = Sheets("DealComparison").Cells(z + 1, 1) Debug.Print Myarray(z) End If Next z
这是完整的代码人,
现在我想要的是,我想检查它是否是空的,如果它不是空的,我必须通过它。
所以我应该在其他声明中提到,这样就可以了。
如果你想确定是否有任何对象是NULL,你应该使用IsNull
函数:
Debug.Print IsNull(Sheets("DealComparison").Cells(z + 1, 1))
要么
Debug.Print IsNull(Sheets("DealComparison"))
来testing一个特定的表单是否为NULL,正如你的问题所要求的那样。
如果要testing零长度string,则使用Len(string) = 0
不是string = ""
进行testing会更有效,因为string以字符数存储在内存中,后跟该字符数和null终止的字符。 这使得string长度比testing字符更容易可用,并避免了涉及的内存分配操作。
If Len(Trim(Sheets("DealComparison").Cells(z + 1, 1))) = 0 Then Myarray(z) = Sheets("DealComparison").Cells(z + 1, 1) Debug.Print Myarray(z) End If
这里有一个很有趣的关于如何将string存储在VB6 / VBA中的评论。
这适用于检查DealComparison表单元格(Row z + 1,Column A)是否为null,也可以使用VBNullString
如:
If Sheets("DealComparison").Cells(z + 1, 1) <> VBNullString Then
因为你的单元格中可能有空格,有时这些空格不会在上面的语句中进行评估。 因此我总是使用TRIMfunction。
If TRIM(Sheets("DealComparison").Cells(z + 1, 1)) <> "" Then
是的,只要“Z”是长整数。 呵呵,有趣的一句话。