I’m really hoping someone can help me to modify the below code – which should automatically populate (with data from my spreadsheet: Sheet2) 4 Textboxes (Reg2 – Reg5) in my Userform AFTER matching the value (Full Name) in my Combobox (Reg1).
The issue is: Currently I am unable to get the
‘Application.WorksheetFunction.VLookup(CLng(Me.Reg1)’ to match TEXT values and NOT numbers – which I believe the ‘CLng’ function has been designed to do.
Unfortunately I only have a very limited knowledge of VBA and got the original code from a tutorial on the internet but I am unable to tweak it to suit my purposes.
Having done a bit of research it seems I possibly need to use a different function like CStr or CVar which I have tried in place of the CLng but with no success.
Please excuse my lack of VBA language / knowledge, but any help is always appreciated.
The code I’m using is:
Private Sub Reg1_AfterUpdate()
'Check to see if value exists
If WorksheetFunction.CountIf(Sheet2.Range("E:E"), Me.Reg1.Value) = 0 Then
MsgBox "This Name does not exist"
Me.Reg1.Value = ""
Exit Sub
End If
'Lookup values based on first control
With Me
.Reg2 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("NINumber"), 2, 0)
.Reg3 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("Trade2"), 3, 0)
.Reg4 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("UTRNumber"), 4, 0)
.Reg5 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("DayRate"), 5, 0)
End With
End Sub
Many thanks in advance
The issue is: Currently I am unable to get the
‘Application.WorksheetFunction.VLookup(CLng(Me.Reg1)’ to match TEXT values and NOT numbers – which I believe the ‘CLng’ function has been designed to do.
Unfortunately I only have a very limited knowledge of VBA and got the original code from a tutorial on the internet but I am unable to tweak it to suit my purposes.
Having done a bit of research it seems I possibly need to use a different function like CStr or CVar which I have tried in place of the CLng but with no success.
Please excuse my lack of VBA language / knowledge, but any help is always appreciated.
The code I’m using is:
Private Sub Reg1_AfterUpdate()
'Check to see if value exists
If WorksheetFunction.CountIf(Sheet2.Range("E:E"), Me.Reg1.Value) = 0 Then
MsgBox "This Name does not exist"
Me.Reg1.Value = ""
Exit Sub
End If
'Lookup values based on first control
With Me
.Reg2 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("NINumber"), 2, 0)
.Reg3 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("Trade2"), 3, 0)
.Reg4 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("UTRNumber"), 4, 0)
.Reg5 = Application.WorksheetFunction.VLookup(CLng(Me.Reg1), Sheet2.Range("DayRate"), 5, 0)
End With
End Sub
Many thanks in advance