简体   繁体   中英

How can I select the active cell and the activecell column with a range for conditional formatting

I am trying to use a conditional formatting based upon my dates in range ("G2:Y2")and my variables for the date are on another sheet. So far I can get the date in range ("G2:Y2") to change color but I can't select the cells in my activecell.column (row 7 to 72) for the same formating. Rows 4, 5 and 6 must remain untouched.

Sub test()
    Dim var1 As Variant 'Date 1
    Dim var2 As Variant 'Date 2
    Dim var3 As Variant 'Date 3
    Dim var4 As Variant 'Date 4
    Dim var5 As Variant 'Date 5
    Dim var6 As Variant 'Date 6
    Dim Z As Long
var1 = LEGENDE.Range("O2").Value
var2 = LEGENDE.Range("O3").Value
var3 = LEGENDE.Range("O4").Value
var4 = LEGENDE.Range("O5").Value
var5 = LEGENDE.Range("O6").Value
var6 = LEGENDE.Range("O7").Value
Z = ActiveCell.[Row]
For Each c In Range("G2:Y2")  'test range
c.Select
Z = ActiveCell.[Row]
If ActiveCell.Value >= var1 And ActiveCell.Value <= var2 Then
ActiveCell.Interior.color = 65535
    'I would like to select the active cell and the activecell column with range 7 to 72 for conditional formatting in excel
Else
End If
Next
End Sub

Generally, avoid using Select and ActiveCell . Refer to c .

For Each c In Range("G2:Y2")
    If c.Value >= var1 And c.Value <= var2 Then
        c.Interior.Color = vbYellow
        Rows("7:72").Columns(c.Column).Interior.Color = vbYellow
    End If
Next

Note that vbYellow is more meaningful than 65535 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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