Solved

Loop and lookup

Posted on 2012-03-27
4
213 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 41

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 41

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 41

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
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,…
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 a scrolling table in Microsoft Excel using the INDEX function.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now