Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

excel vba vlookup

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Excel VBA - Vlookup in a loop

Excel VBA - Vlookup in a loop

ResolvedVersion 2007

Steve has attended:
Excel VBA Intro Intermediate course

Excel VBA - Vlookup in a loop

What is the best way to use the vlookup worksheet function in a loop. For each row in a sheet I want to look up a particular value which is stored in second sheet, return the desired value to the designated target cell, them move down to the next row etc.

I have tried the following but get errors in the code:

intNumRowCount = Sheets("Transactions").Range("A4").CurrentRegion.Rows.Count

'counts number of rows in sheet "Transactions" from cell A4

For intRowCount = 1 To intNumRowCount


Sheets("Transactions").Cells(intRowCount, 16).Value = _
Application.WorksheetFunction.VLookup(Sheets("Transactions").Range("A4").Cells(intRowCount, 6), Sheets("Data").Range("A4:F432"), 6)

intRowCount = intRowCount + 1

Where sheets "Transactions" column 16 is where I want to place the result, based on looking up the value in "Transactions" column 6 in sheets "Data".

RE: Excel VBA - Vlookup in a loop

Hi Steve

Thanks for your question

Could you please advise me as to the nature of the errors that you get, i.e error number and dexcription

Thanks

Stephen

RE: Excel VBA - Vlookup in a loop

Run-time error '1004'
Unable to get the Vlookup property of the WorksheetFunction class.

Thanks

S.

RE: Excel VBA - Vlookup in a loop

Hi Steve

Thanks for that

I had a similar problem a few months back. The first thing I would do is I would give the lookup table a range name and use that in the code rather than the range reference.

This might solve the problem as it will make the references absolute.

If this doesn't work, let me know and we can explore other options

Regards

Stephen

RE: Excel VBA - Vlookup in a loop

I tried what you have suggested by highlighting the range in the spreadsheet and giving it a name. Then using this name in the code instead of the cell references and I still get the same error message.

Thanks

S

RE: Excel VBA - Vlookup in a loop

I have manged to get the answer I require using another online forum.

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Quick Zooming in Excel with rollerball mouses

To zoom in and out of your page hold down the control key and roll the wheel up and down. This will zoom up and down 15% at a time.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.