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!
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: