VLOOKUP with VBA

asyamonique

Well-known Member
Joined
Jan 29, 2008
Messages
1,280
Office Version
  1. 2013
Platform
  1. Windows
Good Day,
On my worksheet column A:B has huge data all the way down.
Column A has ID numbers of the clients, column B has the names and surnames.Could you help me with a code that on my userform I like to put two textboxes,
TextBox2 will show the clients name when the TextBox1 has been updated with new entry.
Many Thanks.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
How about this?
VBA Code:
Private Sub TextBox1_Change()
    Dim ws As Worksheet
    Dim clientID As String
    Dim clientName As String
    Dim lastRow As Long
    Dim i As Long

  
    Set ws = ThisWorkbook.Sheets("Sheet1") '<~~ Replace "Sheet1" with your sheet name

    clientID = TextBox1.Value

    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    For i = 1 To lastRow
        If ws.Cells(i, 1).Value = clientID Then
            clientName = ws.Cells(i, 2).Value
            Exit For
        End If
    Next i

    TextBox2.Value = clientName
End Sub
 
Upvote 0
Solution
How about this?
VBA Code:
Private Sub TextBox1_Change()
    Dim ws As Worksheet
    Dim clientID As String
    Dim clientName As String
    Dim lastRow As Long
    Dim i As Long

 
    Set ws = ThisWorkbook.Sheets("Sheet1") '<~~ Replace "Sheet1" with your sheet name

    clientID = TextBox1.Value

    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    For i = 1 To lastRow
        If ws.Cells(i, 1).Value = clientID Then
            clientName = ws.Cells(i, 2).Value
            Exit For
        End If
    Next i

    TextBox2.Value = clientName
End Sub
Hello again,
If i place another textbox for the column C, is there any way to alter that given code?

TextBox3.Value = ?


Many Thanks
 
Upvote 0
I think I've done it:cool:

VBA Code:
Dim ws As Worksheet
    Dim clientID As String
    Dim clientName As String
    Dim clientName2 As String

    Dim lastRow As Long
    Dim i As Long

  
    Set ws = ThisWorkbook.Sheets("PLAYERS") '<~~ Replace "Sheet1" with your sheet name

    clientID = TextBox1.Value

    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

    For i = 1 To lastRow
        If ws.Cells(i, 1).Value = clientID Then
            clientName = ws.Cells(i, 2).Value
            clientName2 = ws.Cells(i, 3).Value

            Exit For
        End If
    Next i

    TextBox2.Value = clientName
    TextBox3.Value = clientName2
 
Upvote 0

Forum statistics

Threads
1,215,593
Messages
6,125,715
Members
449,254
Latest member
Eva146

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
Back
Top