Search any field

josros60

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

I have a userform (pasting mini sheet) how to modify the file that I can search from any of the fields, also right now keep saying data no found and then display the correct information can it modify to display "data no found" only if is true that the record doesn't exist.


here is the code:

VBA Code:
Private Sub CommandButton1_Click()
Dim x, y As Long

x = Sheets("DIST CODE").Cells(Rows.Count, 1).End(xlUp).Row
With UserForm1

    For y = 2 To x
 If Sheets("DIST CODE").Cells(y, 1).Value = Val(.TextBox1.Value) Then
 
    .TextBox1.Value = Sheets("DIST CODE").Cells(y, 1).Value
    .TextBox2.Value = Sheets("DIST CODE").Cells(y, 2).Value
    .TextBox3.Value = Sheets("DIST CODE").Cells(y, 3).Value
    .TextBox4.Value = Sheets("DIST CODE").Cells(y, 4).Value
    .TextBox5.Value = Sheets("DIST CODE").Cells(y, 5).Value
    .TextBox6.Value = Sheets("DIST CODE").Cells(y, 6).Value
    .TextBox7.Value = Sheets("DIST CODE").Cells(y, 7).Value
    .TextBox8.Value = Sheets("DIST CODE").Cells(y, 8).Value
    Exit Sub
    Else
       
      MsgBox " DATA NOT FOUND"
      

End If
Next y




End With
End Sub

NAVIGATA_DISTRIBUTION SETS-CODES.xlsm
ABCDEFGH
2SAGEIDVENDORACCOUNT #LINE DESCRIPTIONG/L ACCOUNTG/L DESCRIPTIONORG UNITANALYST
3100215ABC COMM.17786-28380,28620, 30862Fixed Data-Customer-Data Services-17786-602110-28-500-100Fixed Data - Customer - Data Services: C - TCOM - ONTNavigata - Carrier CostingMJT
4100109AFX COMMUNICATIONS80017LD - TOLL FREE OTHER SERVICES603202-28-500-100LD - TF - Other : C - TCOM - ONNavigata - Carrier CostingMJT
5100176AIRESPRING1352791LD - Toll DAL : C - TCOM - BC603220-28-500-100LD - Toll DAL : C - TCOM - ONNavigata - Carrier CostingMJT
6100072ANI NETWORKS100226-1001LD - Toll DAL : C - TCOM - BC603220-28-500-100LD - Toll DAL : C - TCOM - ONNavigata - Carrier CostingMJT
7100226ARIN100226LEGACY ANNUAL MAINTENANCE601420-28-500-100Internet - Other - General Internet Transit : C - TCOM - ONNavigata - Carrier CostingJM
8100125AT&T 831831-000-5480-119,264,269Local&VOIP - Other - Services605130-28-500-100Local&VOIP - Other : C - TCOM - ONNavigata - Carrier CostingMJT
9100068BELL TOLL FREECB001945LD - TOLL FREE OTHER SERVICES603202-28-500-100LD - TF - Other : C - TCOM - ONNavigata - Carrier CostingMJT
10100356CCTS(COMMISSION FOR COMPLAINTS FOR TELECOM)611100-28-500-100Local&VOIP - Regulatory Charges C - TCOM - ONNavigata - Carrier CostingMJT
11100120CITY OF NELSON8048HALF RACK COLOCATION RENTAL PER MJTREEMENT601432-28-500-100Network Sites Utilities: C - TCOM - ONNavigata - Carrier CostingMJT
12100313CLOUD (CTI)63015EMBEDDED INTEGRATION/ADMIN FEES-760050-28-550-100Software Licences C - NSE ONNavigata - Carrier CostingMJT
13100271COMWAVEL00004Local&VOIP - Other - Services605130-28-500-10Local&VOIP - Other : C - TCOM - ONNavigata - Carrier CostingMJT
14100116COUNTRY COMM.100116LD - Toll DAL : C - TCOM - BC603220-28-500-100LD - Toll DAL : C - TCOM - ONNavigata - Carrier CostingMJT
DIST CODE
Cell Formulas
RangeFormula
G6,G12:G14,G8:G10G6=VLOOKUP([@SAGEID],Table2,5)
C9,C14C9=VLOOKUP([@SAGEID],Table2[[SAGE ID ]:[Org Unit ]],3)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A3:H52,A53:E53,G53:H53Expression=COLUMN()=CELL("col")textNO
A3:H52,A53:E53,G53:H53Expression=CELL("row")=ROW()textNO


Thank you,
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi,

Please use code as below:

VBA Code:
Private Sub CommandButton1_Click()
Dim x, y As Long
Dim valFound As String
valFound = "N"
x = Sheets("DIST CODE").Cells(Rows.Count, 1).End(xlUp).Row

With UserForm1
    For y = 2 To x
        If Sheets("DIST CODE").Cells(y, 1).Value = Val(.TextBox1.Value) Then
           valFound = "Y"
        
           .TextBox1.Value = Sheets("DIST CODE").Cells(y, 1).Value
           .TextBox2.Value = Sheets("DIST CODE").Cells(y, 2).Value
           .TextBox3.Value = Sheets("DIST CODE").Cells(y, 3).Value
           .TextBox4.Value = Sheets("DIST CODE").Cells(y, 4).Value
           .TextBox5.Value = Sheets("DIST CODE").Cells(y, 5).Value
           .TextBox6.Value = Sheets("DIST CODE").Cells(y, 6).Value
           .TextBox7.Value = Sheets("DIST CODE").Cells(y, 7).Value
           .TextBox8.Value = Sheets("DIST CODE").Cells(y, 8).Value
           Exit Sub
        End If
    Next y
End With

If valFound = "N" Then
      MsgBox " DATA NOT FOUND"
End If

End Sub
 
Upvote 0
Hi,

If it is your requirement to type a value in any one of the 8 textboxes & have the code search the appropriate column in your table (e.g. TextBox3 search Column ACCOUNT # ) for a match & return that rows data then see if this update to your code does what you want

VBA Code:
Private Sub CommandButton1_Click()
    Dim wsDistCode  As Worksheet
    Dim m           As Variant, Search As Variant
    Dim i           As Long, c As Long
    
    Set wsDistCode = ThisWorkbook.Worksheets("DIST CODE")
    
    For c = 1 To 8
        Search = Me.Controls("TextBox" & c)
        If Len(Search) > 0 Then
            If IsNumeric(Search) Then Search = Val(Search)
            m = Application.Match(Search, wsDistCode.Columns(c), 0)
            For i = 1 To 8
                Me.Controls("TextBox" & i).Value = IIf(Not IsError(m), wsDistCode.Cells(CLng(m), i).Text, "")
            Next i
            Exit For
        End If
    Next
    
    If IsError(m) Then MsgBox Search & Chr(10) & " DATA Not FOUND", 48, "Not Found"
    
End Sub

Dave
 
Upvote 0
Thank you for the suggestion, worked good.

if you don't mind one more question how to clear the data on the form to search for next one and also is possible to search using like FORTIS*.* and find anything starting with fortis.

thanks again.
 
Upvote 0
Hi,
If you are referring to my solution then you can, if making a text search, include the wild card in the TextBox

e.g.
FORTIS*

Solution will only return the first matched instance in the range.

Code will clear the form if no search match found but if want to add a clear button

VBA Code:
Private Sub CommandButton2_Click()
    Dim i As Long
    For i = 1 To 8
        Me.Controls("TextBox" & i).Value = ""
    Next i
End Sub

Dave
 
Upvote 0
Solution
Thank you so much, worked perfectly the wildcard search and the clear button.
 
Upvote 0
@josros60 in future please mark the post that solved your problem as the solution, not your post saying it worked. Thanks
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,836
Members
449,096
Latest member
Erald

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