excel 通过每个循环的循环着色单元格

kuuvgm7e  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(124)

我的问题现在得到了回答。我在Excel中选择了几个单元格,例如Range(“B3:F6”)。运行以下代码,我从下面的答案中复制,结果将空单元格着色为灰色:

Sub CellColor()
    
Dim cell As Range

For Each cell In Selection.Cells
    If IsEmpty(cell.Value) Then
        cell.Interior.Color = RGB(217, 217, 217)
    End If
Next cell

End Sub

字符串
(我已经不小心删除了我的假代码。

bnl4lu3b

bnl4lu3b1#

高亮显示范围内的空单元格

快速修复

Sub CellColor()
    
    Dim cell As Range
    
    For Each cell In Selection.Cells
        If IsEmpty(cell.Value) Then
            cell.Interior.Color = RGB(217, 217, 217)
        End If
    Next cell
    
End Sub

字符串

改进

Sub HighlightEmpties()
    
    If Selection Is Nothing Then
        MsgBox "Nothing selected.", vbExclamation
        Exit Sub
    End If
    
    If Not TypeOf Selection Is Range Then
        MsgBox "No range selected.", vbExclamation
        Exit Sub
    End If
    
    Dim urg As Range, cell As Range
    
    For Each cell In Selection.Cells
        If IsEmpty(cell.Value) Then
            If urg Is Nothing Then
                Set urg = cell
            Else
                Set urg = Union(urg, cell)
            End If
        End If
    Next cell
   
    If urg Is Nothing Then
        MsgBox "No empty cells found.", vbInformation
        Exit Sub
    End If
    
    urg.Interior.Color = RGB(217, 217, 217)
    
End Sub

相关问题