Solved

Change Column Ref

Posted on 2012-03-28
3
234 Views
Last Modified: 2012-06-27
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
Comment
Question by:Seamus2626
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 50

Accepted Solution

by:
Ingeborg Hawighorst (Microsoft MVP / EE MVE) earned 500 total points
ID: 37776096
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
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 37776100
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
 

Author Closing Comment

by:Seamus2626
ID: 37776126
Thanks Teylyn,

Thank you too Rory

Seamus
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
NEED TO DO 2 OR 3 OR 4 NUMBERS LOOK UP 2 23
drag and drop (but keep all) lines 11 46
Cannot locate cell 15 42
Embed Excel WS into PPTX 4 26
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question