VBA 根据单元格文本为整行着色
VBA color entire row base on Cell text
示例行 B4 到 AN4 以单元格 AF4 为基础着色为绿色,准备就绪为绿色,进行中为黄色,待处理为红色。
这适用于所有行
未经测试,但按照这些思路应该可以工作:
Sub Tester()
Dim rw As Range, clr As Long
Set rw = ActiveSheet.Range("B4:AN4") 'start row
Do While Application.CountA(rw) > 0 'loop while there's data
Select Case rw.EntireRow.Columns("AF").Value
Case "ready": clr = vbGreen
Case "in progress": clr = vbYellow
Case "pending": clr = vbRed
Case Else: clr = -1
End Select
If clr <> -1 Then
rw.Interior.Color = clr 'apply fill
Else
rw.Interior.ColorIndex = xlNone 'no fill
End If
Set rw = rw.Offset(1, 0) 'next row
Loop
End Sub
根据给定条件突出显示行的特定列
Sub ColourChange()
Dim MySht As Worksheet
Dim MyRng As Range
Set MySht = ThisWorkbook.Sheets("Sheet1")
Set MyRng = MySht.UsedRange.Columns("AF")
For Each cell In MyRng.Cells
Set activeRow = Range("B:AN").Rows(cell.Row)
Select Case cell.Value '// you can add the filter here
Case "Available", "Resell"
activeRow.Interior.Color = XlRgbColor.rgbLightGreen
Case "Deal", "Sold +Excl", "Sold Excl", "Holdback", _
"Pending", "Expired", "Sold CoX"
activeRow.Interior.Color = XlRgbColor.rgbRed
Case "Sold nonX", "Sold NonX"
activeRow.Interior.Color = XlRgbColor.rgbBlue
End Select
Next
End Sub
示例行 B4 到 AN4 以单元格 AF4 为基础着色为绿色,准备就绪为绿色,进行中为黄色,待处理为红色。 这适用于所有行
未经测试,但按照这些思路应该可以工作:
Sub Tester()
Dim rw As Range, clr As Long
Set rw = ActiveSheet.Range("B4:AN4") 'start row
Do While Application.CountA(rw) > 0 'loop while there's data
Select Case rw.EntireRow.Columns("AF").Value
Case "ready": clr = vbGreen
Case "in progress": clr = vbYellow
Case "pending": clr = vbRed
Case Else: clr = -1
End Select
If clr <> -1 Then
rw.Interior.Color = clr 'apply fill
Else
rw.Interior.ColorIndex = xlNone 'no fill
End If
Set rw = rw.Offset(1, 0) 'next row
Loop
End Sub
根据给定条件突出显示行的特定列
Sub ColourChange()
Dim MySht As Worksheet
Dim MyRng As Range
Set MySht = ThisWorkbook.Sheets("Sheet1")
Set MyRng = MySht.UsedRange.Columns("AF")
For Each cell In MyRng.Cells
Set activeRow = Range("B:AN").Rows(cell.Row)
Select Case cell.Value '// you can add the filter here
Case "Available", "Resell"
activeRow.Interior.Color = XlRgbColor.rgbLightGreen
Case "Deal", "Sold +Excl", "Sold Excl", "Holdback", _
"Pending", "Expired", "Sold CoX"
activeRow.Interior.Color = XlRgbColor.rgbRed
Case "Sold nonX", "Sold NonX"
activeRow.Interior.Color = XlRgbColor.rgbBlue
End Select
Next
End Sub