Store vlookup data in macro code?

Posted on 2007-03-28
Last Modified: 2012-06-27
I've got a macro that references a static spreadsheet for a vlookup in my Personal.XLS macro folder.  Now whenever I start or shut down Excel, it asks me if I want to update or save the changes to my Personal Macro Workbook.  Very annoying.  Is there some way to move a simple two column, 60 row dataset into the macro itself?
Question by:Rossamino
  • 4
  • 2
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 18810920
There are ways but it might be easier to set the IsAddin property of your Personal workbook to True - that will eliminate that prompt. To do so, open Personal.xls, press ALT+F11, open the ThisWorkbook code module, press F4 to open the Properties window, find the IsAddin property and set it to True.

LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 18810943
If you want to create a two dimensional array of constants in your macro then use this syntax:

   Dim X As Variant
   X = [{1,2;3,4;5,6}]

where commas separate values in a row and the semicolon separates rows.


Author Comment

ID: 18811715
zorvek, Assuming I do this:

Dim arStates As Variant
arStates = [{Alabama,AL;Alaska,AK;...Wyoming,WY}]

how would a guy go about referencing arStates in a vlookup?

the current code looks like this (its all on one line):
    Range("M2").Value = "=if(iserror(vlookup(L2,'C:\[States.xls]States'!B$1:B$59,1,0)), if(iserror(vlookup(L2,'C:\[States.xls]States'!A$1:B$59,2,0)), concatenate(""zz"" & L2), vlookup(L2,'C:\[States.xls]States'!A$1:B$59,2,0)), vlookup(L2,'C:\[States.xls]States'!B$1:B$59,1,0))"

It seems to me that I'd have to rewrite the vlookup function in VBA and apply the result directly instead of pasting the function.  Yes?
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 18811774
You can't use most Excel functions in VBA with arrays. I suggest using a collection:

   Dim StateNames As Collection
   Dim StateAbreviation As String
   Set StateNames = New Collection
   StateNames.Add "CA", "California"
   StateNames.Add "TX", "Texas"
   StateNames.Add "FL", "Florida"
   StateAbreviation = StateNames("Texas")


Author Comment

ID: 18819378
So when I build this collection, how do I go about using it if I can't use vlookup on the thing?
LVL 81

Accepted Solution

zorvek (Kevin Jones) earned 400 total points
ID: 18819457
Whenever you need to convert a state name to a state abbreviation use the collection like this:

   StateAbreviation = StateNames("Texas")

StateAbreviation will now be "TX".


Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel Formula 5 46
Excel VBA User Form Help 21 29
Excel - click on a cell and have everything in another cell clear 13 33
Formula Help 3 23
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

803 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