Solved

Loop and lookup

Posted on 2012-03-27
4
216 Views
Last Modified: 2012-03-27
Hi,

I have made an attempt at a sub that looks through Column A of a pivot and then attempts to match it to a pivot on another tab, if the value is there the selection turns green, if not, it turns red. My syntax or method is wrong however!

Can anyone offer suggestions?

Thanks
Seamus





Sub ASPAPHOENIXMATCH()

Range("A5").Select



Do Until ActiveCell = "Grand Total"
ActiveCell.Offset(1, 0).Select
With Selection

If(VLOOKUP(selection,'Phoenix Pivot'!A:A,1,0) = true then
Selection.Interior.Color = 65280

End If
Else
Selection.Interior.Color = 255
Loop




End Sub
0
Comment
Question by:Seamus2626
  • 3
4 Comments
 
LVL 42

Accepted Solution

by:
dlmille earned 500 total points
ID: 37772365
Seamus, this should correct your syntax:

Sub ASPAPHOENIXMATCH()
Dim vResult As Variant

    Range("A5").Select

    Do Until ActiveCell = "Grand Total"
        ActiveCell.Offset(1, 0).Select
        With Selection
            
            vResult = Evaluate("VLOOKUP(""" & Selection.Value & """,'Phoenix Pivot'!A:A,1,0)")
            If Not IsError(vResult) Then
                Selection.Interior.Color = 65280
            End If
            
            .Interior.Color = 255
        End With
    Loop

End Sub

Open in new window


Dave
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37772383
Note the double quotes around the Selection.Value.  You could use the WorksheetFunction.Vlooup as well, however I find using Evaluate a bit easier to remember and to validate as I'm coding it.

Setting the variant vResult = Evaluate(Vlookup) will not generate an error, as it stores the result (even an error message) into vResult.

The IsError() is used against the variant vResult to determine whether the VLookUp was successful or not.

Cheers,

Dave
0
 

Author Closing Comment

by:Seamus2626
ID: 37772395
Thanks Dave
0
 
LVL 42

Expert Comment

by:dlmille
ID: 37772422
PS - there are some color constants you can also use with

Selection.Interior.Color =

vbBlack
vbRed
vbGreen
vbYellow
vbBlue
vbMagenta
vbCyan
vbWhite


Cheers,

Dave
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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

808 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