Userform as search tool

ExtraCheese

New Member
Joined
Sep 18, 2020
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
So the situation is as following:
User adds entries with Userform, containing: ID, Name, Address, reference.

All the input data is stored in a different hidden tab.

Now I would want to create a function in a different userform where user can also search the data, for example he searches for the ID, and gets the correspondent name, address and reference. Each data entry is unique, so he should only get one result.

I have this code, but the search function does come up with the wrong results (ID nr; other address etc.).

Private Sub CommandButton1_Click()

Dim id As Long, rowcount As Integer, foundcell As Range

id = TextBox3.Value

rowcount = Sheets("Data").Cells(Rows.Count, 1).End(xlUp).Row

With Worksheets("Data").Range("A2:H" & rowcount)
Set foundcell = .Find(what:=id, LookIn:=xlValues)

If Not foundcell Is Nothing Then
TextBox1.Value = .Cells(foundcell.Column, 1)
TextBox2.Value = .Cells(foundcell.Column, 2)
TextBox4.Value = .Cells(foundcell.Column, 3)
TextBox5.Value = .Cells(foundcell.Column, 7)
TextBox6.Value = .Cells(foundcell.Column, 8)
TextBox7.Value = .Cells(foundcell.Column, 5)
TextBox8.Value = .Cells(foundcell.Column, 6)
Else
TextBox1.Value = ""
TextBox2.Value = ""
TextBox4.Value = ""
TextBox5.Value = ""
TextBox6.Value = ""
TextBox7.Value = ""
TextBox8.Value = ""

End If

End With
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I do not know that's the reason because we do not see layout of data but
VBA Code:
.Cells(RowIndex, ColumnIndex)
so maybe should be
VBA Code:
.Cells(1, foundcell.Column)
 
Upvote 0
Thanks for your reply Kokosek.

Layout of data is:
Box - date - ID - ref - name - address - address 2 - pass nr.

I can't seem to locate the code you mentioned, where do I put it?
 
Upvote 0
Layout of data is:
Box - date - ID - ref - name - address - address 2 - pass nr.
So, ID is in col C, right?
Try:
VBA Code:
Private Sub CommandButton1_Click()

Dim id As String, rowcount As Long, foundcell As Range
Dim n As Long
id = TextBox3.Value

If id <> "" Then
    With Worksheets("Data")
    'ID is in col C
    Set foundcell = .Columns("C").Find(What:=id, LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        If Not foundcell Is Nothing Then
            n = foundcell.Row
            TextBox1.Value = .Cells(n, 1)
            TextBox2.Value = .Cells(n, 2)
            TextBox4.Value = .Cells(n, 3)
            TextBox5.Value = .Cells(n, 7)
            TextBox6.Value = .Cells(n, 8)
            TextBox7.Value = .Cells(n, 5)
            TextBox8.Value = .Cells(n, 6)
        Else
            TextBox1.Value = ""
            TextBox2.Value = ""
            TextBox4.Value = ""
            TextBox5.Value = ""
            TextBox6.Value = ""
            TextBox7.Value = ""
            TextBox8.Value = ""
        End If
    
    End With
End If
End Sub
 
Upvote 0
Solution
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,975
Members
449,095
Latest member
Mr Hughes

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