简体   繁体   English

循环遍历多行

[英]Looping through multiple rows

I have VBA code that goes through aa range and changes the color of cells according to a predefined condition.我有 VBA 代码,它通过一个范围并根据预定义的条件更改单元格的颜色。 The code works for two rows (rows 3 and 4) however, I want to use it another 98 Times.该代码适用于两行(第 3 行和第 4 行),但是,我想再使用它 98 次。

Dim rCell As Excel.Range
Dim rRng As Range        
Set rCell = Range("AS3")

    For Each rCell In ws1.Range("AS3:BG3")
        If rCell.Value < Range("BP3").Value Or rCell.Value > Range("BO3").Value Then
            rCell.Select
            With Selection.Font
                .Color = -16776961
                .TintAndShade = 0
            End With
        End If
    Next rCell

Set rCell = Range("AS4")

    For Each rCell In ws1.Range("AS4:BG4")
        If rCell.Value < Range("BP4").Value Or rCell.Value > Range("BO4").Value Then
            rCell.Select
            With Selection.Font
                .Color = -16776961
                .TintAndShade = 0
            End With
        End If
    Next rCell

If I understand correctly you want range("AS3:BG101") and the IF statement to reference the current rcell row.如果我理解正确,您需要range("AS3:BG101")IF语句来引用当前的rcell行。 This should do it.这应该这样做。 I also removed your extraneous range setting and declaration as well as the selections.我还删除了您无关的范围设置和声明以及选择。

Dim rCell As Range        

    For Each rCell In ws1.Range("AS3:BG101") 'Larger range
        If rCell.Value < Range("BP" & rcell.row).Value Or rCell.Value > Range("BO" & rcell.row).Value Then 'Variable value
            With rcell.Font 'No need to select
                .Color = -16776961
                .TintAndShade = 0
            End With
        End If
    Next rCell

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

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