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
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
VBA Code:
Private Sub cmdSearch_Click()
  
    Dim VesselName As String
   
        VesselName = Val(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
            Exit For
            End If
        Next
       
         If i > lastrow Then
            MsgBox "No results found for '" & txtSearch.Value & "'.", vbInformation, "Search Results"
         End If
   
End Sub
 
Upvote 0
Hi,
Difficult to guess your context ...
But ... may be a potential help : Left(UPPER(yourcell),3) ="F/B"
 
Upvote 0
what I mean is when is search "F/B MM" is says no result found for "F/B MM"

So, to specifically tackle this point ... you need to search for yourtext & "*" ... in order to ensure a thorough scan
 
Upvote 0
So, to specifically tackle this point ... you need to search for yourtext & "*" ... in order to ensure a thorough scan
Nothings change still the same result 🙁

display form.png


Excel Formula:
[RANGE=rs:13|cs:4|v:table|w:Fishing Vessel Data Record.xlsm|cls:xl2bb-200|s:Data|tw:430][XR][XD=ch:0|w:63|cls:h][/XD][XD=ch:0|w:219|cls:h][/XD][XD=ch:0|w:74|cls:h][/XD][XD=ch:0|w:74|cls:h][/XD][/XR][XR][XD=h:c|v:m|bc:808080|ch:31.5]ID[/XD][XD=h:c|v:m|fw:b|bc:D6DCE4]NAME OF FISHING VESSEL[/XD][XD=h:c|v:m|fw:b|bc:DDEBF7]GT[/XD][XD=h:c|v:m|fw:b|bc:FCE4D6]NT[/XD][/XR][XR][XD=ch:15]1[/XD][XD=h:l]F/B MM[/XD][XD=h:c]33.2[/XD][XD=h:c]22.57[/XD][/XR][XR][XD=ch:15]2[/XD][XD=h:l]F/B MM 2[/XD][XD=h:c]7.98[/XD][XD=h:c]5.42[/XD][/XR][XR][XD=ch:15]3[/XD][XD=h:l]F/B MM 3[/XD][XD=h:c]4.44[/XD][XD=h:c]3.02[/XD][/XR][XR][XD=ch:15]4[/XD][XD=h:l]F/B JOSHUA MARI 88[/XD][XD=h:c]40.07[/XD][XD=h:c]27.24[/XD][/XR][XR][XD=ch:15]5[/XD][XD=h:l]F/B JOSHUA MARI 9[/XD][XD=h:c]8.42[/XD][XD=h:c]5.72[/XD][/XR][XR][XD=ch:15]6[/XD][XD=h:l]F/B JOSHUA MARI 7[/XD][XD=h:c]38.95[/XD][XD=h:c]26.5[/XD][/XR][XR][XD=ch:15]7[/XD][XD=h:l]F/B JOSHUA MARI 14[/XD][XD=h:c]11.99[/XD][XD=h:c]8.15[/XD][/XR][XR][XD=ch:15]8[/XD][XD=h:l]F/B JOSHUA MARI 6[/XD][XD=h:c]6.02[/XD][XD=h:c]4.08[/XD][/XR][XR][XD=ch:15]9[/XD][XD=h:l]F/B JOSHUA MARI 2[/XD][XD=h:c]9.61[/XD][XD=h:c]6.54[/XD][/XR][XR][XD=ch:15]10[/XD][XD=h:l]F/B JOSHUA MARI 3[/XD][XD=h:c]10.82[/XD][XD=h:c]7.35[/XD][/XR][XR][XD=ch:15]11[/XD][XD=h:l]F/B JOSHUA MARI IV[/XD][XD=h:c]8.01[/XD][XD=h:c]5.44[/XD][/XR][XR][XD=ch:15]12[/XD][XD=h:l]F/B JOSHUA MARI 12[/XD][XD=h:c]12.09[/XD][XD=h:c]8.22[/XD][/XR][/RANGE]

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, 1).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
            Exit For
        End If
    Next i

    If i > lastRow Then
        MsgBox "No results found for '" & txtSearch.Value & "'.", vbInformation, "Search Results"
    End If
  
     
End Sub
 
Upvote 0
Hi,
Since you have already created a very nice UserForm, would recommend to see:
 
Upvote 0
Everytime I click the search button it says no result found for "f/b" did i missed something in the code? Thanks

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?
 
Upvote 0
Solution
Hi there

If your code is not finding all matches, there could be a couple of reasons why this is happening. Here are a few possible causes and some suggested solutions:
  1. Case sensitivity: Your code might not be finding all matches because it is case sensitive, meaning it will only find matches that have the exact same capitalization as the search term. To make the search case-insensitive, you can use the StrComp function with the vbTextCompare option to compare the values of the search term and the cell value. For example:
    VBA Code:
    For i = 2 To lastRow
        If StrComp(Worksheets("Data").Cells(i, 1).Value, VesselName, vbTextCompare) = 0 Then
            ' code to display the match
        End If
    Next
  2. Hidden characters: Sometimes there may be hidden characters in the data that prevent the code from finding all matches. You can try using the Trim function to remove any leading or trailing spaces from the search term and the cell value. For example:
    VBA Code:
    For i = 2 To lastRow
        If StrComp(Trim(Worksheets("Data").Cells(i, 1).Value), Trim(VesselName), vbTextCompare) = 0 Then
            ' code to display the match
        End If
    Next
  3. Data format: Another possibility is that the data is not formatted consistently, which may prevent the code from finding all matches. For example, if the data in column A contains leading or trailing spaces, the code might not find matches because the search term doesn't match the data exactly. You can try formatting the data to make it consistent, or modify the code to remove any leading or trailing spaces from the data before searching for matches.
If none of these solutions work, you may need to provide more details about the specific data and search term you are using, as well as the expected results, so that we can better understand the issue and provide more targeted guidance.

You can combine the suggestions I provided into the code you provided earlier to make it more robust and accurate. Here's an example of how you can modify your code to include the case-insensitive and leading/trailing space removal suggestions:
VBA Code:
Private Sub cmdSearch_Click()
   
    Dim VesselName As String
    Dim found As Boolean
    
    VesselName = Trim(txtSearch.Text) ' Remove leading/trailing spaces from search term
    lastRow = Worksheets("Data").Cells(Rows.Count, 1).End(xlUp).Row
    found = False
    
    For i = 2 To lastRow
        If StrComp(Trim(Worksheets("Data").Cells(i, 1).Value), VesselName, vbTextCompare) = 0 Then ' Compare search term and cell value, ignoring case and leading/trailing spaces
            txtVName.Text = Worksheets("Data").Cells(i, 2).Value
            txtGT.Text = Worksheets("Data").Cells(i, 3).Value
            txtNT.Text = Worksheets("Data").Cells(i, 4).Value
            found = True
        End If
    Next
    
    If Not found Then
        MsgBox "No results found for '" & VesselName & "'.", vbInformation, "Search Results" ' Display the original search term without leading/trailing spaces
    End If
    
End Sub



By adding the Trim function and the StrComp function with the vbTextCompare option, the code will now remove any leading/trailing spaces from the search term and the cell values, and compare them while ignoring case. This should make the search more robust and accurate.
Note that the message box that displays the "No results found" message now displays the original search term without leading/trailing spaces, since we removed them earlier. This ensures that the user sees the exact search term they entered, even if it contained extra spaces.
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,215,965
Messages
6,127,970
Members
449,414
Latest member
sameri

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