Solved

Loop and lookup

Posted on 2012-03-27
4
220 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
[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
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

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…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

627 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