Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Change Column Ref

Hi,

I have a sub below.

Instead of highlighting as far as Col G, i would like it to find the Column which has ("Grand Total in Row 4 and only highlight as far as that

Thanks
Seamus



Sub ASPASOPHISHKMATCH()
Dim wkb As Workbook
Dim wksA As Worksheet
Dim wksB As Worksheet
Dim rng As Range
Dim r As Range
Dim rFind As Range
Dim rColor As Range

    Set wkb = ThisWorkbook
    Set wksA = wkb.Sheets("ASPA HK")
    Set wksB = wkb.Sheets("Sophis HK Pivot")
   
    Set rng = wksA.Range("A5", wksA.Range("A" & Rows.Count).End(xlUp).Offset(-1, 0))
   
    For Each r In rng
        Set rColor = wksA.Range(r, wksA.Cells(r.Row, "G"))
        Set rFind = wksB.Range("A:A").Find(what:=r.value, LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext)
        If Not rFind Is Nothing Then
            rColor.Interior.Color = vbGreen
        Else
            rColor.Interior.Color = vbRed
        End If
    Next r
   
End Sub
0
Seamus2626
Asked:
Seamus2626
1 Solution
 
Ingeborg Hawighorst (Microsoft MVP / EE MVE)Microsoft MVP ExcelCommented:
Hello,

try this:

Sub ASPASOPHISHKMATCH()
Dim wkb As Workbook
Dim wksA As Worksheet
Dim wksB As Worksheet
Dim rng As Range
Dim r As Range
Dim rFind As Range
Dim rColor As Range

    Set wkb = ThisWorkbook
    Set wksA = wkb.Sheets("ASPA HK")
    Set wksB = wkb.Sheets("Sophis HK Pivot")
    
    Set rng = wksA.Range("A5", wksA.Range("A" & Rows.Count).End(xlUp).Offset(-1, 0))
    
    For Each r In rng
        Set rColor = wksA.Range(r, wksA.Cells(r.Row, WorksheetFunction.Match("Grand Total", wksA.Range("4:4"), 0)))
        Set rFind = wksB.Range("A:A").Find(what:=r.Value, LookIn:=xlValues, lookat:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext)
        If Not rFind Is Nothing Then
            rColor.Interior.Color = vbGreen
        Else
            rColor.Interior.Color = vbRed
        End If
    Next r
    
End Sub

Open in new window


cheers, teylyn
0
 
Rory ArchibaldCommented:
Not for points:

I would do this bit:
WorksheetFunction.Match("Grand Total", wksA.Range("4:4"), 0)

Open in new window


outside the loop as it doesn't change.
0
 
Seamus2626Author Commented:
Thanks Teylyn,

Thank you too Rory

Seamus
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now