Is There a VBA Vlookup Article?

Mike54

Active Member
Joined
Apr 17, 2002
Messages
257
I having a problem with trying to get a Vlookup working in VBA.

In the following code the first one works just fine (all on the same sheet) The second version tries to do exactly the same thing but retrieve data from sheet 2 (data duplicated in identical locations)


Private Sub CommandButton1_Click()
Range("B2") = Application.WorksheetFunction.VLookup(Range("A2"), Range("H1:K20"), 4, False)
End Sub


Private Sub CommandButton2_Click()
Range("Sheet1!B3") = Application.WorksheetFunction.VLookup(Range("sheet2!A3"), Range("H1:K20"), 4, False)
End Sub

I've searched the board for "VBA Vlookup" there are over 300 hits so it seems I'm not alone, I also searched Mr Excel articles normally such a rich resourse, nothing. If anyone can help thank you, or if you know of a good but simple example can you please point me in the right direction.
Many thanks
Mike
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
59,294
Office Version
  1. 365
Platform
  1. Windows
Mike

You have just used some incorrect syntax for referring to cells when also trying to specify the sheet. Try this instead for button 2:

<font face=Courier New><br><br><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CommandButton2_Click()<br>    <SPAN style="color:#00007F">With</SPAN> Sheets("Sheet2")<br>        Sheets("Sheet1").Range("B3").Value = Application.WorksheetFunction. _<br>                VLookup(.Range("A3").Value, .Range("H1:K20"), 4, <SPAN style="color:#00007F">False</SPAN>)<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br></FONT>
 
Upvote 0
L

Legacy 68668

Guest
Where is you CommandButton ?

When the code is written in the Sheet module, any cell reference without its parent object(Sheet) will be made to parent object of the module.

Private Sub CommandButton2_Click()
Range("Sheet1!B3") = Application.WorksheetFunction.VLookup(Range("sheet2!A3"), Range("H1:K20"), 4, False)
End Sub
 
Upvote 0

Mike54

Active Member
Joined
Apr 17, 2002
Messages
257
Thank you all very much, after 8 hours and no reply I wasn't sure I'd get any help. They all look very useful solutions & I can't wait to try but I'm on vacation for the next 8 days so will have to try on my return. Thanks again that's really great.
Regards
Mike
 
Upvote 0

Forum statistics

Threads
1,190,782
Messages
5,982,883
Members
439,803
Latest member
sushilneupane

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
Top