Message Box: No Results Found

OldDogNewTricks

New Member
Joined
Jun 27, 2014
Messages
21
What I Want:
If no rows are named (the input cannot be found), a message box appears saying "No Results Found."

Added bonus if you can help me figure out how to do this:
If rows are named (the input is found), a message box appears saying "The following rows were named:" and then the newly named rows are listed.

What My Code Does:
Click a button.
Window pops-up asking what word I want it to search for. Suppose I type in "CO2."
Excel searches for "CO2" in Column A.
If a row contains that word, the entire row is named "CO2_1." The second time CO2 appears in a row, the row is named "CO2_2." Then it's "CO2_3" and so on.


Please and many thanks!



Code:
  Dim X As Long, Index As Long, Word As String
  Word = InputBox("What word do you want to find and name?")
  If Len(Word) Then
    For X = 1 To Cells(Rows.Count, "A").End(xlUp).Row
      Word = Replace(Word, "-", "_")
      If UCase(Trim(Replace(Cells(X, "A").Value, "-", "_"))) = UCase(Word) Then
        Index = Index + 1
        If Word Like "*#" Then
          Rows(X).Name = "Case2_" & Word & "_" & Index
        Else
          If Word Like "#*" Then
            Rows(X).Name = "Case2_" & Word & Index
            Else: Rows(X).Name = "Case2_" & Word & Index
          End If
        End If
      End If
    Next
  End If
 
Last edited:
I'm not sure about row names. I don't know what the purpose of that is. but I did figure out a way to display a msg box if it finds no results. note highlighted red are the updates.


Code:
  Dim X As Long, Index As Long, Word As String, [COLOR=#ff0000]cnt As Integer
[/COLOR]  Word = InputBox("What word do you want to find and name?")
  If Len(Word) Then
    For X = 1 To Cells(Rows.count, "A").End(xlUp).Row
      Word = Replace(Word, "-", "_")
      If UCase(Trim(Replace(Cells(X, "A").Value, "-", "_"))) = UCase(Word) Then
        Index = Index + 1
       [COLOR=#ff0000] cnt = cnt + 1
[/COLOR]        If Word Like "*#" Then
          Rows(X).name = "Case2_" & Word & "_" & Index
        Else
          If Word Like "#*" Then
            Rows(X).name = "Case2_" & Word & Index
            Else: Rows(X).name = "Case2_" & Word & Index
          End If
        End If
      End If
    Next
  End If
[COLOR=#ff0000]  If cnt = 0 Then
    MsgBox "No results found", vbOKOnly, "Hello"
  End If[/COLOR]

~Matt
 
Upvote 0
I'm not sure about row names. I don't know what the purpose of that is. but I did figure out a way to display a msg box if it finds no results. note highlighted red are the updates.


Code:
  Dim X As Long, Index As Long, Word As String[COLOR=#008000][B], cnt As Integer[/B][/COLOR][COLOR=#ff0000]
[/COLOR]  Word = InputBox("What word do you want to find and name?")
  If Len(Word) Then
    For X = 1 To Cells(Rows.count, "A").End(xlUp).Row
      Word = Replace(Word, "-", "_")
      If UCase(Trim(Replace(Cells(X, "A").Value, "-", "_"))) = UCase(Word) Then
        Index = Index + 1
       [B][COLOR=#008000] cnt = cnt + 1[/COLOR][/B][COLOR=#ff0000]
[/COLOR]        If Word Like "*#" Then
          Rows(X).name = "Case2_" & Word & "_" & Index
        Else
          If Word Like "#*" Then
            Rows(X).name = "Case2_" & Word & Index
            Else: Rows(X).name = "Case2_" & Word & Index
          End If
        End If
      End If
    Next
  End If
[B][COLOR=#ff0000]  If [/COLOR][COLOR=#0000FF]Index[/COLOR][COLOR=#ff0000] = 0 Then
    MsgBox "No results found", vbOKOnly, "Hello"
  End If[/COLOR][/B]
I don't think you need to add the 'cnt' variable... you should be able to use the Index variable that is already in the code... it starts at zero and is incremented only when a row is named. So, I think your modification would also work if the parts I highlighted in green are deleted and your red addition is left in place, but the reference to the (now non-existent) 'cnt' variable is changed to reference the 'Index' variable (shown in blue).
 
Upvote 0

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