excel vba vlookup

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

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

resolvedResolved · Urgent Priority · Version 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:

Shared Conditional Formatting

In a shared workbook, conditional formats applied before the workbook was shared will continue to work; however you cannot modify the existing conditional formats or apply new ones.

View all Excel hints and tips


Server loaded in 0.08 secs.