如何更改我的 VBA 代码以根据文本或值的变化而不仅仅是值的变化来更改行颜色?
我使用以下 VBA 代码(由 Vicky 提供)在每次 A 列中的值发生变化时更改电子表格中行的颜色。唯一的问题是,如果它遇到不是值的内容(例如“10000CO”),它会结束命令,从而不再更改行颜色,并显示错误消息。有什么方法可以操作代码,以便当 A 列中的值或文本更改时它会更改颜色? [注意,我使用的是Excel 2007]
Sub colorize()
Dim r As Long, val As Long, c As Long
r = 4
val = ActiveSheet.Cells(r, 1).Value
c = 19
For r = 4 To ActiveSheet.Rows.Count
If IsEmpty(ActiveSheet.Cells(r, 1).Value) Then
Exit For
End If
If ActiveSheet.Cells(r, 1).Value <> val Then
If c = 19 Then
c = 20
Else
c = 19
End If
End If
ActiveSheet.Rows(r).Select
With Selection.Interior
.ColorIndex = c
.Pattern = xlSolid
End With
val = ActiveSheet.Cells(r, 1).Value
Next
End Sub
I am using the following VBA code (provided by Vicky) to change the color of the rows in a spreadsheet each time the value in column A changes. The only problem is, if it runs into something that isn't a value (such as "10000CO") it ends the command, thus no longer changing the row colors, and comes up with an error message. Is there any way to manipulate the code so that it changes the color when the value OR text in column A changes? [Note that I am using Excel 2007]
Sub colorize()
Dim r As Long, val As Long, c As Long
r = 4
val = ActiveSheet.Cells(r, 1).Value
c = 19
For r = 4 To ActiveSheet.Rows.Count
If IsEmpty(ActiveSheet.Cells(r, 1).Value) Then
Exit For
End If
If ActiveSheet.Cells(r, 1).Value <> val Then
If c = 19 Then
c = 20
Else
c = 19
End If
End If
ActiveSheet.Rows(r).Select
With Selection.Interior
.ColorIndex = c
.Pattern = xlSolid
End With
val = ActiveSheet.Cells(r, 1).Value
Next
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将 val 的类型从 Long 更改为 String
Try to change type of val from Long to String