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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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