[英]Highlighting Customer (A5) when B5=1. Loop for B5 to B5000
This was working for a while in Excel 13.10 (32 bit), but needs to work in excel 2007, then suddenly it stopped working and gave a bug error 1004 on the line for R2.Interior.Color = RGB (255,153,255)
and only after looping a few time.这在 Excel 13.10(32 位)中工作了一段时间,但需要在 excel 2007 中工作,然后突然停止工作并在
R2.Interior.Color = RGB (255,153,255)
行上出现错误错误 1004,并且仅在之后循环几次。 Here is my code:这是我的代码:
Sub COL_Hilight()
Dim i As Long, r1 As Range, r2 As Range
For i = 5 To 5000
Set r1 = Range("B" & i)
Set r2 = Range("A" & i)
If r1.Value = 1 Then r2.Interior.Color = RGB(255, 153, 255)
Next i
End Sub
not sure why it worked for perhaps 5-7 times, then suddenly stopped.不知道为什么它可能工作了 5-7 次,然后突然停止了。 Please help.
请帮忙。
I can't understand why you don't use conditional formatting for this:我不明白你为什么不为此使用条件格式:
Manually it looks like this:手动看起来像这样:
You can automate this as follows:您可以按如下方式自动执行此操作:
Range("B5:B5000").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=(A5=1)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = RGB(255, 153, 255)
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
The property Range.Interior.Color
is not available in Excel 2007. It was first introduced in Excel 2010.属性
Range.Interior.Color
在 Excel 2007 中不可用。它在 Excel 2010 中首次引入。
However, an alternative way is to use conditional formatting, which is available for your version (2007) of Excel.但是,另一种方法是使用条件格式,它适用于您的 Excel 版本 (2007)。
Sub COL_Hilight()
Dim ws as Worksheet
Set ws = THisworkbook.Worksheets("Sheet1")
With ws
Range("A5:A5000").FormatConditions.Add Type:=xlExpression, Formula1:="=$B5=1"
Range("A5:A5000").FormatConditions(1).Interior.Color= RGB(255, 153, 255)
Range("A5:A5000").FormatConditions(1).StopIfTrue = False
End with
End Sub
You may freely play around with this format to make it more dynamic, however the code above should add the conditional format according to your criteria through VBA.您可以随意使用此格式以使其更具动态性,但是上面的代码应根据您的条件通过 VBA 添加条件格式。
Keep in mind that this assumes that your worksheet is named Sheet1
.请记住,这假定您的工作表名为
Sheet1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.