1

I have a watch range with certain cells highlighted yellow. For each of these cells, there is a corresponding reference value all located within one column.

I want to highlight all cells in my target range red for which the value in the corresponding reference cell matches the value in the target cells.

The code I've come up with is as follows, but there is some sort of a compile error I am unable to fix. Apparently the watch range can't consist of "multiple ranges".

Sub Highlight_pairAB()
Dim WatchRange As Range, Target As Range, cell As Range, ref As Range
Set Target = Range("Y3:Y274", "AC3:AC274") 'change column ref as required
Set WatchRange = Range("B3:B274", "E3:E274", "H3:H274", "K3:K274")
Set RefRange = Range("A3:A102")

For Each cell In Target.Cells
    If Application.WorksheetFunction.CountIf(WatchRange, cell.Value) > 0 Then
        cell.Interior.ColorIndex = 3
        For Each watchCell In WatchRange.Cells
            If watchCell.Interior.ColorIndex = 6 And RefRange.Value = Target.Value Then: targetCell.Interior.ColorIndex = 3
        Next watchCell
    Else: cell.Interior.ColorIndex = xlNone
    End If
Next cell
End Sub

2 Answers 2

1

Just in case this helps: Change:

Set WatchRange = Range("B3:B274", "E3:E274", "H3:H274", "K3:K274") 

To:

Set WatchRange = Range("B3:B274,E3:E274,H3:H274,K3:K274")
Sign up to request clarification or add additional context in comments.

Comments

1

Another thing you could try would be to replace

For Each watchCell In WatchRange.Cells

with

For Each area In WatchRange.Areas
     For Each watchCell In area.Cells

edit: you also need two "Next" statements for it to match up. So be sure to make it

    Next watchCell
Next area

1 Comment

This looks promising but now I get an error of "Else without If" here: "Else: cell.Interior.ColorIndex = xlNone"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.