VBA:属性判断

IsArray、IsDate、IsEmpty、IsError、IsMissing、IsNull、IsNumeric、IsObject作用及语法

函数 语法 注释
IsArray IsArray(varname) 判断是不是数组
IsDate IsDate(expression) 判断是不是日期格式
IsEmpty IsEmpty(expression) 判断单元格是否为空
IsError IsError(expression) 错误值
IsNull IsNull(expression) 是否为空
IsNumeric IsNumeric(expression) 是否是数字
IsObject IsObject(identifier) 是否表示某个对象的变量
IsMissing IsMissing(argname) 否已将可选Variant 参数传递给过程
     

示例:

Sub aa()
    Dim str As String
    str = "12345"
    If IsNumeric(str) Then
        MsgBox "字符串是一个数值。"
    Else
        MsgBox "字符串不是一个数值。"
    End If
End Sub

判断单元格是否为空值

Sub CheckIfCellIsEmpty()
    Dim cell As Range
    Set cell = ThisWorkbook.Sheets("Sheet1").Range("A1")
 
    If IsEmpty(cell.Value) Then
        MsgBox "单元格A1为空"
    Else
        MsgBox "单元格A1不为空"
    End If
End Sub

如果不是空值,用

 If(Not IsEmpty(cell.Value)) Then
        MsgBox "单元格A1不是空"
End If