[英]Effect Certain Column/column ranges in a VBA Code
我正在处理下面的 VBA 代码,以根据特定单元格中指定的文本更改单元格的颜色。 我已经让代码在活动工作表中工作,但是当我运行我的代码时,它会擦除我在原始电子表格中的所有颜色(不知道为什么)。 我只想将此代码应用于表中的某些列。 EX 仅更改第 12-1000 行之间 P 列和 Y 列中这些值的颜色...
这是我正在使用的代码:
Sub changeColor()
Dim itm As Range
Application.ScreenUpdating = False
ActiveSheet.UsedRange.Offset(1).Interior.ColorIndex = xlColorIndexNone
For Each itm In ActiveSheet.UsedRange.Offset(1)
If Not IsError(itm) Then
With itm
Select Case .Value2
Case "GREEN", "green", "Green"
.Interior.Color = XlRgbColor.rgbLightGreen
Case "RED", "red", "Red"
.Interior.Color = XlRgbColor.rgbRed
Case "Yellow", "yellow", "Yellow"
.Interior.Color = XlRgbColor.rgbYellow
End Select
End With
End If
Next
Application.ScreenUpdating = True
End Sub
@“但是当我运行我的代码时,它会擦除我在原始电子表格中的所有颜色(不知道为什么)。”
这一行是说,使用范围内的每个单元格,擦除该颜色:
ActiveSheet.UsedRange.Offset(1).Interior.ColorIndex = xlColorIndexNone
然后使用 Tim Williams 在评论中所说的内容,指定要循环的单元格范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.