你应该这么玩EXCEL-检查你工作表中的空值或0值

第一种:

代码如下:

  Dim rng As Range
    Dim R As Range
    Dim TRng As Range
    Set rng = Range("A1:D10")
    For Each R In rng
        If R.Value = "" Or R.Value = 0 Then
            If TRng Is Nothing Then
                Set TRng = R
            Else
                Set TRng = Application.Union(TRng, R)
            End If
        End If
    Next
    MSGBOX TRng.address(0, 0)

第二种:

代码如下:

  Dim rng As Range
    Dim cell As Range
    Dim address As String
      
    '设置要检查的区域
    Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1:D10")
      
    '循环检查每个单元格
    For Each cell In rng
        '检查单元格是否为空或值为0
        If IsEmpty(cell.Value) Or cell.Value = 0 Then
            '将地址存储到字符串变量中
            address = cell.address
            '显示MsgBox并告知地址
            MsgBox "单元格 " & address & " 是空或值为0"
        End If
    Next cell

那如何标注红色那?

代码如下:

Dim rng As Range
    Dim cell As Range
    Set rng = ThisWorkbook.Sheets("Sheet1").Range("A1:D10")
    For Each cell In rng
        If cell.Value = 0 Then
            cell.Interior.Color = RGB(255, 0, 0)  ' RGB颜色码,红色为(255, 0, 0)
        End If
    Next cell

本期分享结束。