Vlookup macro

Alka Bajaj

New Member
Joined
Apr 5, 2011
Messages
44
Hello Expert,

I have a vlookup macro which

Current Functionality
  • Can only execute on a sheet on which macro is created.
  • Have to copy two reference sheet to that WB for Macro to refer
Expected Requirement
  • Can run the macro code on any sheet, without copying the code to that sheet.
  • If I do not copy the two reference sheet.
Code Used:
Sub lookup()
Columns("D:D").Insert Shift:=xlToRight
Columns("E:E").Insert Shift:=xlToRight
With Range("D2:D250000")
.Formula = "=VLOOKUP(C2,ZDT_HR_CON_PERN!$I$2:$AG$50000,25,FALSE)"
.Value = .Value
End With
With Range("E2:E250000")
.Formula = "=VLOOKUP(D2,CSG_Desc!$A$2:$B$20,2,FALSE)"
.Value = .Value
End With
Range("D1") = "CSG ID"
Range("E1") = "CSG"
End Sub

Appreciate your guidance to execute currect requirement.

Regards,
Alka Bajaj
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hello Expert,

I have a vlookup macro which

Current Functionality
  • Can only execute on a sheet on which macro is created.
  • Have to copy two reference sheet to that WB for Macro to refer
Expected Requirement
  • Can run the macro code on any sheet, without copying the code to that sheet.
  • If I do not copy the two reference sheet.
Code Used:
Sub lookup()
Columns("D:D").Insert Shift:=xlToRight
Columns("E:E").Insert Shift:=xlToRight
With Range("D2:D250000")
.Formula = "=VLOOKUP(C2,ZDT_HR_CON_PERN!$I$2:$AG$50000,25,FALSE)"
.Value = .Value
End With
With Range("E2:E250000")
.Formula = "=VLOOKUP(D2,CSG_Desc!$A$2:$B$20,2,FALSE)"
.Value = .Value
End With
Range("D1") = "CSG ID"
Range("E1") = "CSG"
End Sub

Appreciate your guidance to execute currect requirement.

Regards,
Alka Bajaj

If the code is placed in a Module, you should be able to run it on any sheet? I'm using 2003. For 2003:

Copy your sub procedure Tools>Macro>Visual Basic Editor>Select the project name (should be the name of the workbook)>Insert>Module and paste the code.

For your second question, If I understand, I think you only need to revised your formula to indicate the workbook as well, like so:

"=VLOOKUP(C2,'[WORKBOOK NAME.xls]ZDT_HR_CON_PERN!$I$2:$AG$50000,25,FALSE)"

The workbook with the reference sheets should be open when you run the macro though.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,753
Members
452,940
Latest member
rootytrip

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