Programming for Everybody: search button in userform Excel and display all data in one label With Source Code

search button in userform Excel and display all data in one label With Source Code



 Source Code:

Private Sub CommandButton1_Click()

If TextBox1.Text = "" Then

MsgBox "Please Enter ID"

Exit Sub

End If

Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Sheet6")

Dim i As Integer

For i = 1 To ws.UsedRange.Rows.Count

If TextBox1.Text = ws.Cells(i, 1).Value Then

Label2.Caption = "First name: " & ws.Cells(i, 2).Value & vbCrLf & "" _

& "Last name: " & ws.Cells(i, 3).Value & vbCrLf & "" _

& "Marks: " & ws.Cells(i, 4).Value

Exit Sub

Else

Label2.Caption = "No data found"

End If

Next i

End Sub



No comments:

Post a Comment