SOLVED

VBA code for Training - No error but nothing appears after pressing search button

Copper Contributor

Hello, 

 

I am learning how to code to work around training management better, however I am stuck on the following code:

 

Sub CommandButton1_Click()
Dim cust_id As String
Dim lastrow As Integer
Dim i As Integer

cust_id = Trim(TextBox1.Text)
lastrow = Worksheet("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lastrow

If TextBox1.Text = Worksheet("Sheet1").Cells(i, 1).Value Then

TextBox2.Text = Worksheet("Sheet1").Cells(i, 2).Value
TextBox3.Text = Worksheet("Sheet1").Cells(i, 3).Value
TextBox4.Text = Worksheet("Sheet1").Cells(i, 4).Value
End If

Next

End Sub

 

After typing and pressing the search button for an ID e.g, 310, the other results (First Name, Last Name, etc) are not appearing. 

 

Can you please help? 

 

Thank you. 

Ejman

2 Replies
best response confirmed by Ejman435 (Copper Contributor)
Solution

@Ejman435 

Sub CommandButton1_Click()
Dim cust_id As String
Dim lastrow As Integer
Dim i As Integer

cust_id = Trim(TextBox1.Text)
lastrow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lastrow

If TextBox1.Text = Worksheets("Sheet1").Cells(i, 1).Value Then

TextBox2.Text = Worksheets("Sheet1").Cells(i, 2).Value
TextBox3.Text = Worksheets("Sheet1").Cells(i, 3).Value
TextBox4.Text = Worksheets("Sheet1").Cells(i, 4).Value
End If

Next

End Sub

It works in my sheet after changing Worksheet("Sheet1") to Worksheets("Sheet1").

textbox.JPG

Dear Quadruple_Pawn,

Thank you so much for your help!!! It's solved :D
1 best response

Accepted Solutions
best response confirmed by Ejman435 (Copper Contributor)
Solution

@Ejman435 

Sub CommandButton1_Click()
Dim cust_id As String
Dim lastrow As Integer
Dim i As Integer

cust_id = Trim(TextBox1.Text)
lastrow = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To lastrow

If TextBox1.Text = Worksheets("Sheet1").Cells(i, 1).Value Then

TextBox2.Text = Worksheets("Sheet1").Cells(i, 2).Value
TextBox3.Text = Worksheets("Sheet1").Cells(i, 3).Value
TextBox4.Text = Worksheets("Sheet1").Cells(i, 4).Value
End If

Next

End Sub

It works in my sheet after changing Worksheet("Sheet1") to Worksheets("Sheet1").

textbox.JPG

View solution in original post