Userform Search

josros60

Well-known Member
Joined
Jun 27, 2010
Messages
780
Office Version
  1. 365
Hi,

have the code below attached to search button but when click nothing happen no error and no search.

here is the code:

VBA Code:
Private Sub btnSearch_Click()
Dim RecordRow As Long
    Dim RecordRange As Range

    ' Turn off default error handling so Excel does not display
    ' an error if the record number is not found
    On Error Resume Next
        
        ' Find the row in the table that the record is in
        RecordRow = Application.Match(CLng(txtvendor.Value), Range("DISTRIBUTION_SET_G-L_CODING[Record]"), 0)
        
        ' Set RecordRange to the first cell in the found record
        Set RecordRange = Range("DISTRIBUTION_SET_G-L_CODING").Cells(1, 1).Offset(RecordRow - 1, 0)

        ' If an erro has occured i.e the record number was not found
        If Err.Number <> 0 Then
    
            ErrorLabel.Visible = True
            On Error GoTo 0
            Exit Sub
        
        End If
    
    ' Turn default error handling back on (Let Excel handle errors from now on)
    On Error GoTo 0
    
    ' If the code gets to here the record number was found
    ' Hide the error message 'Not Found'
    ErrorLabel.Visible = False
    ' and populate the form fields with the record's data
    txtvendor.Value = RecordRange(1, 1).Offset(0, 1).Value
    txtcurrency.Value = RecordRange(1, 1).Offset(0, 1).Value
    txtbPO.Value = RecordRange(1, 1).Offset(0, 1).Value
    txtapprover.Value = RecordRange(1, 1).Offset(0, 1).Value
    txtGLcode.Value = RecordRange(1, 1).Offset(0, 1).Value
    txtLineDes.Value = RecordRange(1, 1).Offset(0, 1).Value
    txttax.Value = RecordRange(1, 1).Offset(0, 1).Value
    txtAccTer.Value = RecordRange(1, 1).Offset(0, 1).Value
    txtOrgUnit.Value = RecordRange(1, 1).Offset(0, 1).Value
    txt.Value = RecordRange(1, 1).Offset(0, 1).Value
End Sub

thank you
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
What is "DISTRIBUTION_SET_G-L_CODING" is it a table or a named range?
Is the name correct?

Rich (BB code):
  RecordRow = Application.Match(CLng(txtvendor.Value), Range("DISTRIBUTION_SET_G-L_CODING[Record]"), 0)
  ' Set RecordRange to the first cell in the found record
  Set RecordRange = Range("DISTRIBUTION_SET_G-L_CODING").Cells(1, 1).Offset(RecordRow - 1, 0)

You can remove the On Error Resume Next statement from the code, try and see what error comes up and on which line of the macro it stops.

Another way would be to search the sheet and the column. What is the name of the sheet and in which column to do the search.
 
Upvote 0
I assume it is a table, check that the name is correct: "DISTRIBUTION_SET_G_L_CODING".
Try the following:

VBA Code:
Private Sub btnSearch_Click()
  Dim f As Range
  
  Set f = Range("DISTRIBUTION_SET_G_L_CODING[Record]").Find(txtvendor.Value, , xlValues, xlWhole)
  If Not f Is Nothing Then
    txtcurrency.Value = f.Offset(0, 1).Value
    txtbPO.Value = f.Offset(0, 2).Value
    txtapprover.Value = f.Offset(0, 3).Value
    txtGLcode.Value = f.Offset(0, 4).Value
    txtLineDes.Value = f.Offset(0, 5).Value
    txttax.Value = f.Offset(0, 6).Value
    txtAccTer.Value = f.Offset(0, 7).Value
    txtOrgUnit.Value = f.Offset(0, 8).Value
    txt.Value = f.Offset(0, 9).Value
  Else
    MsgBox "The number does not exist"
  End If
End Sub
 
Upvote 0
Hi,

thank you for the code.

I tried it but getting this error:

Run-time error 1004
Method 'Range" of object'_Global' failed

and highlight this line:

VBA Code:
Set f = Range("DISTRIBUTION_SET_G-L_CODING[Record]").Find(txtvendor.Value, , xlValues, xlWhole)

Thanks again.
 
Upvote 0
is table "Table5"

And should you look in the "Record" column?

Try:

VBA Code:
Private Sub btnSearch_Click()
  Dim f As Range
  
  Set f = Range("Table5[Record]").Find(txtvendor.Value, , xlValues, xlWhole)
  If Not f Is Nothing Then
    txtcurrency.Value = f.Offset(0, 1).Value
    txtbPO.Value = f.Offset(0, 2).Value
    txtapprover.Value = f.Offset(0, 3).Value
    txtGLcode.Value = f.Offset(0, 4).Value
    txtLineDes.Value = f.Offset(0, 5).Value
    txttax.Value = f.Offset(0, 6).Value
    txtAccTer.Value = f.Offset(0, 7).Value
    txtOrgUnit.Value = f.Offset(0, 8).Value
    txt.Value = f.Offset(0, 9).Value
  Else
    MsgBox "The number does not exist"
  End If
End Sub

If it doesn't work, put here an image of your table where you can see the name of the table and its columns.
 
Upvote 0
Hi,

here sample:

SAMPLE.xlsx
ABCDEFGHIJ
1SAGEIDVENDORCURRENCYPO_NUMBERAPPROVERGL_CODELINE_DESCRIPTIONTAX_GROUPACCOUNT#_TERMSORG_UNIT
2CANCANADIAN NUMBERING ADMINISTRATIONCanadian DollarsWael Attalla/Blaisia Semega611100-13-500-000 Local&Voip - Regulatory Charges : R - NWPLN - GAnnual share of costs - Numbering Administration in Canada fHST 13%Primus Approved by email
3MACFUEMACNAMARA FUELSCanadian DollarsINV 9430Glen Gregory/Walter Bakovich783110-03-535-000 Repairs & Maintenance: G - FIELD - GOPERATIONAL VARIANCE - 90 DAYS INSPECTION LETTERHSTONPrimus - Service Operations
4MAGENTMAGENTO AN ADOBE COMPANYU.S. DollarsNOV 10-2021-NOV 09-2022BRAD FISHER/SHAUN RANDALL/NAV KAINTHRAI760050-03-330-000 Software Licenses: G - MARK - GGOLD SUPPORT NOV 09-2021-NOV 09-2022Exemption Tax on US$NET30Primus - Marketing Web Costs
5MALWARMALWAREBYTES INC.U.S. DollarsNOV 30 2021-NOV 29 -2022Sheldon Pepin/George Zarif760050-03-330-000 Software Licenses: G - IT - GMALWAREBYTES INDENT SITE-NOV 30 2021-NOV 29-20222HST 13% USDNET30Primus - SD
6MASAHMMasroor Ahmad LoneCanadian DollarsGerry Vanderpost/Hayden Sookram711143-03-215-000 Subcontractors - Outsource : G - FIN - GCONTRACTOR FOR BILLING-MARCH 2022Exemption TaxDURPrimus - Finance billing
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A2:J6Expression=COLUMN()=CELL("col")textNO
A2:J6Expression=CELL("row")=ROW()textNO


Thank you,
 
Upvote 0
I guess then that the table name is Table5 and you want to search in column "B" (Vendor).
If so, then try like this:


Rich (BB code):
Private Sub btnSearch_Click()
  Dim f As Range
  
  Set f = Range("Table5[Vendor]").Find(txtvendor.Value, , xlValues, xlWhole)
  If Not f Is Nothing Then
    txtcurrency.Value = f.Offset(0, 1).Value
    txtbPO.Value = f.Offset(0, 2).Value
    txtapprover.Value = f.Offset(0, 3).Value
    txtGLcode.Value = f.Offset(0, 4).Value
    txtLineDes.Value = f.Offset(0, 5).Value
    txttax.Value = f.Offset(0, 6).Value
    txtAccTer.Value = f.Offset(0, 7).Value
    txtOrgUnit.Value = f.Offset(0, 8).Value
    txt.Value = f.Offset(0, 9).Value
  Else
    MsgBox "The number does not exist"
  End If
End Sub

If not, then you need to be more specific and provide the full information, or share your file for review.

You could upload a copy of your file to a free site such www.dropbox.com or google drive. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
Solution
Hi,

Tried but when I search it complete the fields in the wrong text where it should vendor name put the PO number and so on (attaching image)

thank you
 

Attachments

  • USERFORM.PNG
    USERFORM.PNG
    17.1 KB · Views: 5
Upvote 0
I don't know what you are putting in the txtvendor textbox.
If I put "MACNAMARA FUELS" in the txtvendor textbox, according to your example in post #7, it puts the data correctly for me.
1655914166435.png


But from the beginning I am only guessing how your data is.
So help me to be able to help you, share your file to verify how you have the data.

You could upload a copy of your file to a free site such www.dropbox.com or google drive. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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