Categories
Excel Training VBA Training

How to use a VLOOKUP function in Excel VBA

VLOOKUP is one of the most useful and versatile functions in Excel. As you work further with macros it’s not uncommon to make your create an Excel VBA VLOOKUP macro. With this you get the ability to reference your tables of data, but automated.

Wait, what’s a VLOOKUP function?

The Vertical Lookup is one of Excel’s most popular commands. It’s most common use allows you retrieve data from another table of data based on a key value. For example, in one Excel sheet you may have a list of one customer’s invoice numbers, and in another sheet a list of all your invoice numbers plus other columns, such as amount, customer and invoice date. A VLOOKUP function can use the invoice number as a reference point to extract one or more other related columns of data. This avoids sloppy copy-and-paste and ensures the data remains up to date.

excel vlookup function
An example of a simple VLOOKUP retrieving a ticket price for a given country.

There are other smart uses of the VLOOKUP, such as being able to search for duplicates, group values into buckets and check to see if items exists but this is enough detail for now.

For a more thorough discussion of the VLOOKUP function, check out our article here. Even better, come on one of our Excel Advanced courses!

So what about using Excel VBA VLOOKUPs?

You can retrieve data from sheet to sheet programmatically using VBA alone, usually with nested FOR NEXT loops and variables to track your current cell position. These can be a bit fiddly and the learning curve can be a little steep (if you want to learn how to do this check out our Excel VBA Introduction / Intermediate course).

an example of excel vba vlookup style functionality
The CopyRecords macro is simulating VLOOKUP-style functionality.

Luckily, VBA provides you with the Application.WorksheetFunction method which allows you to implement any Excel function from within your macro code.

So if your original VLOOKUP in cell B2 was something like this:

=VLOOKUP(Input!A2, Data!A1:X200, 5, FALSE)

The VBA version would look like this:

Range("B2") = Application.WorksheetFunction.VLookup(Sheets("Input").Range("A2"), Sheets("Data").Range("A1:X200"), 5, False)

Notice a couple of things:  I had to insert the Sheets and Range objects so VBA could properly interpret it.

I want a sneakier version!

Don’t want to do that Sheets and Ranges business? You can adopt a little-known punctuation trick in VBA that converts things into cell references: the square brackets. Did you know you can turn this:

Range("A1") = "Fred"

into

[A1] = "Fred"

With a little lateral thinking we can do the same to our VLOOKUP:

[B2] = [VLOOKUP(Input!A2, Data!A1:X200, 5, FALSE)]

Make it more robust

The code lines above will do the bare minimum. They’ll get the job done. What if you grab the result of the VLOOKUP and store it in a variable?

result = [VLOOKUP(Input!A2, Data!A1:X200, 5, FALSE)]

And then throw an unexpected value at it?

vba error 1004
Of course! Error 1004! Why didn’t I see that coming?

Let’s add a little error-handling so your code doesn’t come to a screeching halt. There’s a number of ways you can test this, and various things you can do with the error, so here’s only one suggestion:

On Error GoTo MyErrorHandler:

  result = [VLOOKUP(Input!A2, Data!A1:X200, 5, FALSE)]

MyErrorHandler:
  If Err.Number = 1004 Then
    MsgBox "Value not found"
  End If

Or possibly

On Error GoTo MyErrorHandler:

  result = [VLOOKUP(Input!A2, Data!A1:X200, 5, FALSE)]

MyErrorHandler:
  If Err.Number = 1004 Then
    result = ""
  End If

The first example throws the error right up in your face. The second one is a ‘silent’ error that pushes a null string into the result variable. Not usually advisable as you can’t actually spot the problem but on some occasions you just want to move past the code issue. You could replace the empty string with your own custom error message.

Make it dynamic

This is all well and good but what if your data grows and shrinks? You should be on the lookout for dynamic methods. You can consider things such as dynamic range names and similar, but here’s a VBA option you can consider:

    Dim ws As Worksheet
    Dim LastRow As Long
    Dim TargetRange As Range

    On Error GoTo MyErrorHandler:

    Set ws = Sheets("Data")

    LastRow = ws.Cells(Rows.Count, "X").End(xlUp).Row
    Set TargetRange = ws.Range("A1:X" & LastRow)

    result = Application.WorksheetFunction.VLookup(Sheets("Input").Range("A2"), TargetRange, 5, False)

    MsgBox result

MyErrorHandler:
    If Err.Number = 1004 Then
      MsgBox "Value not found"
    End If

Note I went back to standard sheet and range references; they’re tricky to mix and match with square bracket notation.

What’s happening here? We set up three variables, to house the sheet name, last row and final range. Then we calculate what the last used row is with the “xlUp” method. This is equivalent to pressing CTRL + up on the keyboard when working in Excel. This finds the last row on the worksheet, and finally we set this as the range used.

There’s lots of variations on this, depending on whether you need dynamic columns too and how regular your data is, but this is a great method for getting you started.

So there it is, from soup to nuts, ways to implement VLOOKUP functions in Excel VBA.

Looking for help on your VBA projects? We offer the UK’s largest schedule of VBA training events, with all versions and levels trained. The Introduction and Intermediate levels will give you all the tools you need to get started, while the Advanced course will allow you to hook up other Office applications and communicate with databases. We can also visit your offices to deliver training, or consult on your projects. We can also offer Access VBA and Word VBA too.

Read about our Excel VBA training solutions

5 Things You Didn’t Know Excel VBA Could Do

Categories
Excel Training Technology

Could a single spreadsheet bankrupt your business?

Dangerous Spreadsheets

That’s not a headline designed just to capture your attention. It’s a very real possibility.

Spreadsheets can be powerful. They’re full of functionality yet easy to use. Everyone can create and use a spreadsheet without much in the way of training.

But all those benefits are also the dangers. Spreadsheets weren’t designed to carry the burden of vast computations or huge numbers of contributing editors. If a business is over-reliant on spreadsheets – and there are often much better applications for the job – then it is up to the business to ensure that what’s being calculated is correct.

The consequences of one wrong answer can be catastrophic. We offer our Top 6 questions to ask about the critical spreadsheets in your business to protect you from failure.

1.           What’s a critical spreadsheet?

Because spreadsheets are so often created by the user population rather than the IT function the way they are developed, updated and shared is rarely regulated. This doesn’t really matter for personal use. But it becomes a serious problem where the business depends on a spreadsheet to determine budgets, pricing, billing, reporting or any other core financial data.

“There have been high-profile cases of organisations mis-reporting their finances because of errors in spreadsheets in recent years,” reports Gary Fenn, trainer at Best Software Training London (Best STL). “Not only does this hurt the company’s credibility but ever-tightening compliance regulations could mean huge penalty fines as well.”

2            Where are our critical spreadsheets?

Depending on the size of the business there could be tens of thousands of spreadsheets sitting on hard drives or in the cloud. Many will be of little risk to the business, but the challenge is to find those that could do serious damage.

“A thorough audit will start to give you those answers” says Gary Fenn. The way you do that could be anything from simply asking people to report what they’ve got through to using automated tools to scan network resources.”

If you have to comply with regulations such as Sarbanes Oxley, then flowcharting business processes with associated spreadsheets could greatly help the auditing process.

3.           Can we identify the risky spreadsheets?

The next step is to determine which of those spreadsheets are putting your business in jeopardy. The best way is to formalise a process for ranking spreadsheets according their risk. How significant would an error in that spreadsheet be to the business? How many people can update the data and the formulas in spreadsheet? How complex is the spreadsheet and how much data is it manipulating?

“It’s not enough to know where the critical spreadsheets are,” adds Fenn. “You need to create an inventory of what they are, why they were created and who can update them. And keep that inventory itself updated.”

4.           How can we protect those spreadsheets?Protect Your Spreadsheets

An obvious route is to fully utilise the security features available in spreadsheets such as access privileges and protected areas to ensure only the right people can make changes.

That’s a start, says Gary Fenn but “it’s more important is set up processes and policies which everyone can understand and that ensure spreadsheets are doing what they are intended to do without interference.”

This could include an agreed method for versioning spreadsheets across the organisation, approval processes for spreadsheet changes, and a procedure for verifying data that is input to the spreadsheet.

5.           Are we doing enough to satisfy compliance demands?

Financial regulations around the world, such as Sarbanes Oxley, have become much tighter since the global crisis. Yet there are few regulations specifically around spreadsheets. Despite that business leaders are expected to understand the risk and demonstrate how they are managing it.

“It’s all about responsibilities,” says Gary Fenn. “Make sure that both IT and individuals understand their roles in risk management, and that there is a process for reporting risk to the board. If you have a role already dealing with financial risk management, assign spreadsheets to be another aspect of their portfolio.”

6.           What about future spreadsheets?

Gary Fenn has advice for anyone about to embark on a new spreadsheet. “You should take time to design a spreadsheet before it’s used in a real business situation. Think about the final outcome – the question the spreadsheet is answering – and work backwards from there. If the spreadsheet has a business-critical role, conduct a financial risk and benefit assessment. This will help you decide the correct proportion of resources to allocate to the creation and management of the process.”