how to make VlookUp on VBA excel?

NewBieSteffie
NewBieSteffie used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ryan ChongSoftware Team Lead

Commented:
how to make VlookUp on VBA excel?
try use:

Application.WorksheetFunction.VLookup

Open in new window

Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
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

Author

Commented:
Thank you for this.

It worked!!!
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial