Sub Find_Empty_Cells()
Dim tbl As ListObject
Dim i As Long
'Declare table name
Set tbl = ThisWorkbook.Worksheets("Sheet1").ListObjects("tblTest")
With tbl
'Loop Through Every Row except header in Table
For i = 1 To tbl.Range.Rows.Count - 1
'Check column 1
If .DataBodyRange(i, 1).Value = "" Then
.DataBodyRange(i, 1).Interior.ColorIndex = 3
End If
'Check column 3
If .DataBodyRange(i, 3).Value = "" Then
.DataBodyRange(i, 3).Interior.ColorIndex = 3
End If
Next i
End With
End Sub
Sub Colour_Empty_Cells_Red()
Dim empty_cells As Range
On Error Resume Next
With Range("A1").CurrentRegion
Set empty_cells = Union(.Columns(1), .Columns(3)).SpecialCells(xlCellTypeBlanks)
End With
On Error GoTo 0
If Not (empty_cells Is Nothing) Then empty_cells.Interior.Color = vbRed
End Sub
Sub Colour_Empty_Cells_Red()
Dim empty_cells As Range, ws As Worksheet
On Error Resume Next
For Each ws In ActiveWorkbook.Worksheets
With ws.Range("A1").CurrentRegion
Set empty_cells = Union(.Columns(1), .Columns(3)).SpecialCells(xlCellTypeBlanks)
End With
If Not (empty_cells Is Nothing) Then empty_cells.Interior.Color = vbRed
Next
On Error GoTo 0
End Sub
2条答案
按热度按时间q8l4jmvw1#
在
Sheet1
上创建一个表,将表命名为tblTest
,然后尝试以下操作:qmelpv7a2#
不需要遍历所有单元格。
您可以使用
SpecialCells(xlCellTypeBlanks)
来标识区域中的所有空单元格,并且可以使用Columns(1)
和Columns(3)
来定位给定区域的第1列和第3列。将这两个范围结合起来将给予第1列和第3列中所有为空的单元格。把这些涂成红色要在工作簿中的每个工作表上运行代码,请创建一个
For Each..
循环,循环遍历每个工作表,然后使用以下命令将更改的工作表引用包含在Range(“A1”)用途: