简体   繁体   English

将条件格式应用于可变范围

[英]Applying conditional formatting to variable range

I want this macro to select range C6:N6, apply a format condition, step 7 and so on until the end of my list. 我希望此宏选择范围C6:N6,应用格式条件,然后执行步骤7,以此类推,直到列表末尾。 When i execute it, it bings me an error regarding the objects. 当我执行它时,它会提示我有关对象的错误。

I think my problem is on the following part: 我认为我的问题出在以下部分:

For i = 6 To lastRow Step 7
    Range(C & i, N & i).Select 

This way I want to make the ranges variable. 这样我想使范围可变。 Below is my code: 下面是我的代码:

Sub test1()
' Coluna C

Dim lastRow As Long
lastRow = Cells(Rows.Count, 1).End(xlUp).Row ' Pega ultima celula com valores na coluna C

For i = 6 To lastRow Step 7
    Range(C & i, N & i).Select 'what am i missing?

         With Selection.Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
        Formula1:="=0"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False


Next i


End Sub

Instead of keeping this into comments, here is an answer to your problem. 不用在评论中,而是对您的问题的解答。
Your code is considering both C and N as variables, when it should consider them as literals. 您的代码在将CN视为变量时将其视为变量。 For that, you need to enclose them in double quotes( " ). So instead of 为此,您需要将它们括在双引号( " )中。

Range(C & i, N & i).Select

You need to do 你需要做

Range("C" & i, "N" & i).Select

There is also one problem with your lastRow assignment. 您的lastRow分配也存在一个问题。 Based on your comments you want to get the last cell with values from column C , which is column 3 , but you are passing the wrong index for colum in here: 根据您的评论,您想要从C列(即第3列)获得最后一个具有值的单元格,但是您在此处传递了错误的colum索引:

lastRow = Cells(Rows.Count, 1).End(xlUp).Row ' Pega ultima celula com valores na coluna C

Instead, you should be using 相反,您应该使用

lastRow = Cells(Rows.Count, 3).End(xlUp).Row ' <-- column C is index 3

I'm adding this because you might find issues if your column A is not properly filled or doesn't have the same amount of data as the desired column C 我要添加此信息是因为,如果您的A列未正确填充或数据量与所需的C列相同,则可能会发现问题

Besides, as greatly suggested by BruceWayne (off topic: nice username, by the way), you should avoid .Select completely in your code. 此外,正如BruceWayne所建议的(当然,主题是:不错的用户名),您应该避免在代码中完全选择.Select You can work with With blocks, similarly to what you are already doing, and also use proper reference to your cells and ranges, instead of relying on the active worksheet. 您可以使用With块,类似于您已经在做的事情,并且还可以对单元格和范围使用正确的引用,而不是依赖活动的工作表。 With that, you would end up with a code more like the following 这样,您最终将获得类似于以下内容的代码

Sub test1()
' Coluna C

    Dim lastRow As Long
    With ThisWorkbook.Worksheets("YourSheetName")
        lastRow = .Cells(.Rows.Count, 3).End(xlUp).row ' Pega ultima celula com valores na coluna C

        For i = 6 To lastRow Step 7
            With .Range("C" & i, "N" & i)

                With .Interior
                    .Pattern = xlNone
                    .TintAndShade = 0
                    .PatternTintAndShade = 0
                End With
                .FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
                    Formula1:="=0"
                .FormatConditions(.FormatConditions.Count).SetFirstPriority
                With .FormatConditions(1).Interior
                    .PatternColorIndex = xlAutomatic
                    .Color = 255
                    .TintAndShade = 0
                End With
                .FormatConditions(1).StopIfTrue = False
            End With

        Next i
    End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM