Solved

Loop and lookup

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel Conditional Statements 11 40
Office 2016 without internet 6 38
Manipulate Range in Excel VBA 6 26
Excel filter on tab not showing any entries? 5 23
Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

832 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