使用VBA在Excel中着色一系列单元格,可以通过以下步骤实现: 打开Excel,并在需要着色的工作表中选择要着色的单元格范围。Sub ColorCells() Dim rng As Range Dim cell As Range Dim colorIndex As Integer ' 设置要着色的单元格范围 Set rng = Selection ' 设置要使用的颜色索引(1-56) colorIndex = 3 ' 遍历...
Set maxColorCell = Nothing ' 遍历范围中的每个单元格 For Each cell In rng ' 检查单元格是否有颜色 If cell.Interior.Color <> RGB(255, 255, 255) Then ' 这里假设白色为无颜色状态 ' 检查单元格的值是否大于当前最大值 If cell.Value > maxVal Then ' 更新最大值和最大值对应的彩色单元格 maxVal...
Note: You can also use built-in VBA color constants such asvbYellowinstead of RGB. So instead ofmyrange.Interior.Color = RGB(255, 255, 0), you can usemyrange.Interior.Color = vbYellow VBA Code to Change Cell Color Based on Cell Value We can use VBA code to change the background c...
LastRange.Interior.ColorIndex = xlNone ' 清除上次的突出显示 Set LastRange = Nothing ' 清除上次突出显示的区域 End If '检查选定的单元格是否在数据区域内 If Not Intersect(currCell, dataRange) Is Nothing Then Set currRange = Union(currCell.EntireRow, currCell.EntireColumn) Set currRange = Intersec...
.Range("A1:E10").Interior.color = RGB(r, g, b) Set currCell = .Range("I" & .Rows.Count).End(xlUp).Offset(1) currCell.Clear currCell.Value = "RGB(" & r & ", " & g & "," & b & ")" currCell.Interior.color = RGB(r, g, b) ...
Cells(4, 6).Offset(0, 1).Interior.Color = Cells(4, 6).Interior.Color 上面VBA执行后则将(4,6)单元格的颜色向右偏移一格填充(4,6)颜色 将(0,1)换成(0,-1)则是向左偏移填充颜色 执行下面的宏代码 Sub a()For i = 2 To 100 '假设有99行要涂色的数据 If Cells(i,...
Private Sub Worksheet_SelectionChange(ByVal Target As Range) Me.Cells.FormatConditions.Delete With Me.Cells.FormatConditions.Add(Type:=xlExpression, Formula1:="=CELL(""row"")=ROW()") .Interior.Color = RGB(255, 0, 0) End With With Me.Cells.FormatConditions.Add(Type:=xlExpres...
Get Cell Color Function Function returns the active cell interior or font color index, regardless of whether it was set by regular or Conditional Formatting.
SumByColor = SumByColor + rCell.Value '累加 End If Next rCell End Function '按单元格填充颜色计数 'Count_range计数区域,Ref_color参考颜色所在单元格 Function CountByColor(Count_range As Range, Ref_color As Range) As Long Dim rg As Range '定义变量为单元格 For Each rg In Count_range '...
代码语言:vba 复制 Sub SetCellColorBasedOnTextValue() Dim cell As Range For Each cell In Selection Select Case cell.Value Case "文本值1" cell.Interior.Color = RGB(255, 0, 0) '设置红色 Case "文本值2" cell.Interior.Color = RGB(0, 255, 0) '设置绿色 ...