简体   繁体   English

当 B5=1 时突出显示客户 (A5)。 B5 至 B5000 循环

[英]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.

相关问题 进行循环而没有“预定义”单元格(即仅“ B”而不是“ b5”) - doing a loop without “predefining” cells (ie just “B” instead of “b5”) excel = SIN(MOD($ B5; 23)/ 23 * 2 * PI())错误 - excel =SIN(MOD($B5;23)/23*2*PI()) error 函数= VLOOKUP(MAX(B5:B11); A4:B11; 1; 1)Excel 2010 - Function =VLOOKUP(MAX(B5:B11);A4:B11;1;1) Excel 2010 每次从具有绿色背景和值 = 0 的 Range("B1 : B5") 单元格计数时添加 range("B6") 的值 - Adding value of range("B6") everytime it counts from Range("B1 : B5") cell that has Green Background and value = 0 每次单击按钮时,VBA 中是否有一种简单的方法可以将 Range(“B2:B5, FB2:OR5”) 移动超过 1? - Is there an easy way in VBA to shift the Range(“B2:B5, FB2:OR5”) over 1 each time you click a button? 需要公式以在所有可用工作表上添加单元格B5(将创建未来工作表)并在单元格D5上显示总计 - Need formula to add cell B5 on all available sheets (future sheets will be created) and display the total on cell D5 从子文件夹excel文件中复制来自非连续单元格(A1,B5,C6)的数据并粘贴到父文件夹中找到的主文件中 - copy data from non contiguous cells (A1, B5, C6) from subfolders excel files and paste in master file found in parent folder 如何创建 Excel VBA 循环以在单元格 A5、B4、C3、D2、E1 中打印文本(“x”) - How to create a excel VBA loop to print text("x") in cell A5,B4,C3,D2,E1 如果AJ列包含“客户”,则在VBA中的B:B中应用过滤器 - IF Column AJ contains “Customer” then Apply Filter in B:B Else In VBA 如何读取范围('A5:B10')并使用 openpyxl 将这些值放入数据框中 - How can I read a range('A5:B10') and place these values into a dataframe using openpyxl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM