提问人:Nuun 提问时间:11/16/2023 最后编辑:Nuun 更新时间:11/16/2023 访问量:54
通过VBA为每个循环着色单元格
Coloring Cells via VBA for each loop
问:
我的问题现在得到了解答。我在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
(我已经不小心删除了我的错误代码。
答:
2赞
VBasic2008
11/16/2023
#1
突出显示区域中的空单元格
快速修复
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
评论
Set rrange = Selection.Cells
当您处理对象时。(尽管该变量在这里毫无意义)Dim rrange As Range: Set rrange = Selection.Cells
Dim c As Range
c.Interior.Color = RGB(217, 217, 217)