how to make VlookUp on VBA excel?

Hello Experts!

I am new to VBA and still exploring.
I want to make a macro in which if the user will click a button it will let the user to input all the attributes for VlookUp and then the macro will search the VLookUp value and paste it to the active cell.

Thank you in advance!
NewBieSteffieInternAsked:
Who is Participating?
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You may give this a try...
Sub VLookup()
Dim cell As Range, LookupCell As Range, LookupRange As Range, Target As Range
Dim ColIndex As Integer
Set cell = ActiveCell
On Error Resume Next

Set LookupCell = Application.InputBox("Select cell with Lookup value.", Type:=8)
If LookupCell Is Nothing Then
    MsgBox "You didn't select the cell with Lookup value.", vbExclamation
    Exit Sub
End If

Set LookupRange = Application.InputBox("Select Lookup Table Range.", Type:=8)
If LookupRange Is Nothing Then
    MsgBox "You didn't select the Lookup Range.", vbExclamation
    Exit Sub
End If

ColIndex = Application.InputBox("Please specify the column index from the Lookup Range " & LookupRange.Address(0, 0), Type:=1)
If ColIndex = 0 Then
    MsgBox "You didn't specify the Column Index.", vbExclamation
    Exit Sub
End If

Set Target = LookupRange.Columns(1).Find(what:=LookupCell.Value, lookat:=xlWhole)

If Not Target Is Nothing Then
    cell.Value = Target.Offset(0, ColIndex - 1).Value
Else
    MsgBox "The Lookup value " & LookupCell.Value & " was not found in the range " & LookupRange.Columns(1).Address & ".", vbExclamation
End If
End Sub

Open in new window

0
 
Ryan ChongCommented:
how to make VlookUp on VBA excel?
try use:

Application.WorksheetFunction.VLookup

Open in new window

0
 
NewBieSteffieInternAuthor Commented:
Thank you for this.

It worked!!!
0
 
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.