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

x
?
Solved

Loop and lookup

Posted on 2012-03-27
4
Medium Priority
?
226 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 2000 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

963 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