No result found

xainthowell

New Member
Joined
Mar 3, 2023
Messages
24
Office Version
  1. 2013
Platform
  1. Windows
Everytime I click the search button it says no result found for "f/b" did i missed something in the code? Thanks

VBA Code:
Private Sub cmdSearch_Click()
   
    Dim VesselName As String
    
        VesselName = (txtSearch.Text)
        lastRow = Worksheets("Data").Cells(Rows.Count, 1).End(xlUp).Row
        
        For i = 2 To lastRow
        
            If Worksheets("Data").Cells(i, 1).Value = VesselName Then
            txtVName.Text = Worksheets("Data").Cells(i, 2).Value
            txtGT.Text = Worksheets("Data").Cells(i, 3).Value
            txtNT.Text = Worksheets("Data").Cells(i, 4).Value
            End If
        Next
        
         If Not found Then
            MsgBox "No results found for '" & txtSearch.Value & "'.", vbInformation, "Search Results"
         End If
    
End Sub

Fishing Vessel Data Record.xlsm
ABCDE
1IDNAME OF FISHING VESSELGTNTCO
21F/B MM (MOTHER BOAT)33.222.57OK
32F/B MM 2 ( FISH CARRIER)7.985.42OK
43F/B MM 3 ( FISH CARRIER)4.443.02OK
54F/B JOSHUA MARI 88 (MOTHER BOAT)40.0727.24OK
65F/B JOSHUA MARI 9 (FISH CARRIER)8.425.72OK
76F/B JOSHUA MARI 7 (MOTHER BOAT)38.9526.5OK
87F/B JOSHUA MARI 14(FISH CARRIER)11.998.15OK
98F/B JOSHUA MARI 6 (FISH CARRIER) 6.024.08OK
109F/B JOSHUA MARI 2 (FISH CARRIER)9.616.54OK
1110F/B JOSHUA MARI 3 (FISH CARRIER)10.827.35OK
1211F/B JOSHUA MARI IV(FISH CARRIER)8.015.44OK
1312F/B JOSHUA MARI 12 (FISH CARRIER)12.098.22OK
1413F/B JOSHUA MARI 16 (MOTHER BOAT)46.331.48OK
Data
Cell Formulas
RangeFormula
A2:A14A2=ROW()-1
 
Hi all
You are making two mistakes:

a) You are looking in column A when you should be looking in column B.
b) And what you write in 'txtSearch' its code looks for it as the only content of the cell when that data is a part of the cell.

Do you understand what is happening to you?
You're right I'm looking in column A instead in column B. Thanks

display form.png


VBA Code:
Private Sub cmdSearch_Click()
 
    Dim VesselName As String
    Dim lastRow As Long
    Dim i As Long

    VesselName = UCase(txtSearch.Text) ' Convert txtSearch value to uppercase
    lastRow = Worksheets("Data").Cells(Rows.Count, 1).End(xlUp).Row

    For i = 2 To lastRow
        If UCase(Worksheets("Data").Cells(i, 2).Value) Like VesselName & "*" Then ' Compare in uppercase
            txtVName.Text = Worksheets("Data").Cells(i, 2).Value
            txtGT.Text = Worksheets("Data").Cells(i, 3).Value
            txtNT.Text = Worksheets("Data").Cells(i, 4).Value
            txtCO.Text = Format((Worksheets("Data").Cells(i, 5).Value), "dd-mmm-yy")
            txtCVR.Text = Format((Worksheets("Data").Cells(i, 6).Value), "dd-mmm-yy")
            txtMSMC.Text = Format((Worksheets("Data").Cells(i, 7).Value), "dd-mmm-yy")
            Exit For
        End If
    Next i

    If i > lastRow Then
        MsgBox "No results found for '" & txtSearch.Value & "'.", vbInformation, "Search Results"
    End If
 
    
End Sub
 

Attachments

  • display form.png
    display form.png
    63.5 KB · Views: 7
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
VBA Code:
Private Sub cmdSearch_Click()
    For i = 2 To lastRow
        If UCase(Worksheets("Data").Cells(i, 2).Value) Like VesselName & "*" Then ' Compare in uppercase
    Next i
Now it's perfect: I'm glad I could help you! ;)
 
Upvote 0
Mario R 's solution is working fine ... and fixes your initial code ...

However, given what you are currently building using a loop is less efficient than using Filter ... ;)
 
Upvote 0
Mario R 's solution is working fine ... and fixes your initial code ...

However, given what you are currently building using a loop is less efficient than using Filter ... ;)
I already make a little modification of the whole code and it works perfectly now. Thanks to all for the help 🥹
 
Upvote 0

Forum statistics

Threads
1,215,903
Messages
6,127,650
Members
449,394
Latest member
fionalofthouse

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