Help with VBA code for a userform

Lah

New Member
Joined
Jan 29, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi there,

I am extremely new to VBA (As in I found it today). I am using a code and I want to change it. Can the search form bring up anything equal to and like text that is written in the search box of the form.

For context I am creating a userform from a database and want the form to search for specific records and populate the form once the record is found. It's likely people will not write the name of the record in the search box exactly as it is written in the database therefore I want the code to have a like condition (if that's a thing) so that it will pull anything that is equal to or like whatever is written in the search bar.

If anyone can help I would really appreciate it!!!

At the moment this is what I have:-

Private Sub cmdSearch_Click()
Dim x as long
Dim y as long
X= Sheets ("Worksheet").Cells(y,1).value =txtSearch.Text Then
txtID=Sheets ("Worksheet").Cells (y,1).Value
txtName=Sheets ("Worksheet").Cells (y,2).Value
cmbGender=Sheets ("Worksheet").Cells (y,3).Value
txtAddress=Sheets ("Worksheet").Cells (y,4).Value
txtContact=Sheets ("Worksheet").Cells (y,5).Value

End if
Next y

End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi
welcome to forum

See if this update to your code goes in right direction for you

Excel Formula:
Private Sub txtSearch_Change()
    Dim r           As Long
    Dim ws          As Worksheet
   
    Set ws = ThisWorkbook.Worksheets("Worksheet")
   
    For r = 2 To ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
       
        If UCase(ws.Cells(r, 1).Text) Like UCase(Me.txtSearch.Text) & "*" Then
           
            Me.txtID = ws.Cells(r, 1).Text
            Me.txtName = ws.Cells(r, 2).Text
            Me.cmbGender = ws.Cells(r, 3).Text
            Me.txtAddress = ws.Cells(r, 4).Text
            Me.txtContact = ws.Cells(r, 5).Text
           
            Exit For
           
        End If
       
    Next r
End Sub

I placed code in the txtSearch change event - this should start populating your textboxes as you type & negates need for commandbutton but note, your code would not clear previous found entry when you clear the search textbox but this could be modified if code does indeed, do what you want.


Dave
 
Upvote 0

Forum statistics

Threads
1,224,812
Messages
6,181,091
Members
453,021
Latest member
Justyna P

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