How to use the vlookup from selected value in Listbox and come to Textbox in VBA?


I would like to ask this code;

Private Sub ListBox1_Click()

Dim ws As Worksheet Dim LastRow As Long Dim TargetRange As Range

On Error GoTo MyErrorHandler:

Set ws = Sheets("MasterFile")

LastRow = ws.Cells(Rows.count, "M").End(xlUp).Row
Set TargetRange = ws.Range("A1:M" & LastRow)

result = Application.WorksheetFunction.VLookup(Sheets("TextBox15").Range("L2"), TargetRange, 12, False)

MsgBox result

MyErrorHandler: If Err.Number = 1004 Then MsgBox "Value not found" End If

[enter image description here][1][enter image description here][1]

End Sub

I have a string or value in ListBox1 and I want to use the vlookup and the result will come to my Textbox1 and so on. can you please check my code if there is missing or mistake? see the attached Excel and userform for your reference.enter image description here

- - Source
comments powered by Disqus