我有一个有标题的表,第一列有学生姓名。表格的其余部分有学生成绩。
到目前为止,我需要突出显示所有带零的单元格
Sub HighLightZeros()
Dim region As Range
Set region = ActiveSheet.Range("a1").End(xlDown)
If region.Value = 0 Then
region.Interior.Color = vbYellow
Else
region.Interior.ColorIndex = xlColorIndexNone
End If
End Sub
此外,我必须制作宏,以便当我在表格上添加更多分数时,它仍会突出显示所有零值。我无法弄清楚如何选择整个表格。
答案 0 :(得分:0)
你可以喜欢以下
Sub HighLightZeros()
With ActiveSheet.UsedRange
With .Resize(, .Columns.Count - 1).Offset(, 1) 'reference all "used" cells except column A ones
.Interior.ColorIndex = xlColorIndexNone ' uncolor referenced range
.Replace what:=0, lookat:=xlWhole, replacement:="XXX" ' replace 0's with "XXX"
.SpecialCells(XlCellType.xlCellTypeConstants, xlTextValues).Interior.Color = vbYellow ' color referenced range cells with text content in yellow
.Replace what:="XXX", lookat:=xlWhole, replacement:=0 ' replace "XXX"'s with 0
End With
End With
End Sub
尝试在问题中建立一些来自骨架的工作代码,它可能是
Sub HighLightZeros2()
Dim region As Range, cell As Range
Set region = Range("a1", Range("A1").End(xlDown))
For Each cell In region
If cell.Value = 0 Then
cell.Interior.Color = vbYellow
Else
cell.Interior.ColorIndex = xlColorIndexNone
End If
Next
End Sub
但它只扫描第一列单元格
答案 1 :(得分:0)
这比@ DisplayName的方法效率低,但另一种方法。假设table是一个实际的表(List对象。使用Ctrl + T创建)
Option Explicit
Sub HighlightZeroes()
Dim table As ListObject
Dim currentCell As Range
Set table = ThisWorkbook.Worksheets("Sheet1").ListObjects("Table1") 'change as required
For Each currentCell In table.DataBodyRange
With currentCell
.Interior.ColorIndex = xlColorIndexNone
If .Value = 0 Then .Interior.Color = vbYellow
End With
Next currentCell
End Sub