Dynamic VBA vlookup

Justin8506

New Member
Joined
Apr 7, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hi There

How do I create a dynamic vlookup vba. Got stuck at creating a dynamic vlookup vba code. Below was the initial code.

VBA Code:
Sub Vlookup()

Range("K2") = "=VLOOKUP(RC[-5], ('C:\Users\Desktop\Aging report\Mar22\[Book1.xlsx]Sheet3'!R2C5:R100C9),5,FALSE)"


End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Welcome to the Board!

How do you want it dynamic?
What should change and what does that depend on?

A few other issues:

1. NEVER, EVER use reserve words (names of existing Excel or VBA functions, properties, methods, objects etc) like "VLOOKUP" as the name of your procedures, functions, or variables. This could lead to errors or unexpected behaviors, as when you refer to "VLOOKUP", Excel cannot be sure if you mean the built-in function or your procedure.

2. It looks like your code is trying to set a formula using R1C1 referencing.
As such, it needs to start:
Rich (BB code):
Range("K2").FormulaR1C1 = ...
 
Upvote 0
Welcome to the Board!

How do you want it dynamic?
What should change and what does that depend on?

A few other issues:

1. NEVER, EVER use reserve words (names of existing Excel or VBA functions, properties, methods, objects etc) like "VLOOKUP" as the name of your procedures, functions, or variables. This could lead to errors or unexpected behaviors, as when you refer to "VLOOKUP", Excel cannot be sure if you mean the built-in function or your procedure.

2. It looks like your code is trying to set a formula using R1C1 referencing.
As such, it needs to start:
Rich (BB code):
Range("K2").FormulaR1C1 = ...
thanks for the response Joe4. I managed to figure out the dynamic vlookup code after multiple trial and error
 
Upvote 0
thanks for the response Joe4. I managed to figure out the dynamic vlookup code after multiple trial and error
Glad you figured it out.
Bear in mind what I told you about naming your procedures, as what you had originally can cause yourself a lot of headaches.
 
Upvote 0
Glad you figured it out.
Bear in mind what I told you about naming your procedures, as what you had originally can cause yourself a lot of headaches.
hi Joe4, would you be able to advise why am I getting a 400 error when I have the vba code running fine for sheet1 but not sheet3 (after changing the sheet name)?
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,215,032
Messages
6,122,770
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top