Macro that looks for the name of active tab in the data on the sheet

shahdelsol

Active Member
Joined
Jul 21, 2009
Messages
276
Office Version
  1. 365
Platform
  1. Windows
I am looking for macro that searches for the name of the tab in the data of the sheet and if found it turns it in bold and highlights it in red.
Let's say the name of sheet/tab is 0123456, I would like to look for 0123456 or 123456 in the sheet and if found , make it bold and red and if not found the popup would say no match found. The name of tab is usually a number that sometimes starts with zero(s), if that matters. However the zeros may or may not appear in the data itself. I appreciate that anyone can help!
 
Last edited:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Try this:
Code:
Sub TabNameInSheet()
Dim R As Range, fAdr As String
If Val(ActiveSheet.Name) = 0 Then
      With ActiveSheet.UsedRange
            Set R = .Find(ActiveSheet.Name, , xlValues, xlPart, , xlNext)
            If R Is Nothing Then
                  MsgBox "Sheet name not found on sheet"
            Else
                  fAdr = R.Address
                  Do
                        With R
                              .Font.Bold = True
                              .Interior.Color = vbRed
                        End With
                        Set R = .FindNext(R)
                  Loop While Not R Is Nothing And R.Address <> fAdr
            End If
      End With
Else
      With ActiveSheet.UsedRange
            Set R = .Find(Val(ActiveSheet.Name), , xlValues, xlPart, , xlNext)
            If R Is Nothing Then
                  MsgBox "Sheet name not found on sheet"
            Else
                  fAdr = R.Address
                  Do
                        With R
                              .Font.Bold = True
                              .Interior.Color = vbRed
                        End With
                        Set R = .FindNext(R)
                  Loop While Not R Is Nothing And R.Address <> fAdr
            End If
      End With
End If
End Sub
 
Upvote 0
Thank you so much. It did exactly what I expected. I just have a question. It seems like the code has been repeated twice. Is this on purpose or just a typo?
Try this:
Code:
Sub TabNameInSheet()
Dim R As Range, fAdr As String
If Val(ActiveSheet.Name) = 0 Then
      With ActiveSheet.UsedRange
            Set R = .Find(ActiveSheet.Name, , xlValues, xlPart, , xlNext)
            If R Is Nothing Then
                  MsgBox "Sheet name not found on sheet"
            Else
                  fAdr = R.Address
                  Do
                        With R
                              .Font.Bold = True
                              .Interior.Color = vbRed
                        End With
                        Set R = .FindNext(R)
                  Loop While Not R Is Nothing And R.Address <> fAdr
            End If
      End With
Else
      With ActiveSheet.UsedRange
            Set R = .Find(Val(ActiveSheet.Name), , xlValues, xlPart, , xlNext)
            If R Is Nothing Then
                  MsgBox "Sheet name not found on sheet"
            Else
                  fAdr = R.Address
                  Do
                        With R
                              .Font.Bold = True
                              .Interior.Color = vbRed
                        End With
                        Set R = .FindNext(R)
                  Loop While Not R Is Nothing And R.Address <> fAdr
            End If
      End With
End If
End Sub
 
Last edited:
Upvote 0
Thank you so much. It did exactly what I expected. I just have a question. It seems like the code has been repeated twice. Is this on purpose or just a typo?
You are welcome.

Because your "numbers" may have leading zeroes (012345), I didn't know whether they are true numbers or just text entries. The first part of the code looks for text entries, while the second part (which is slightly different from the first) deals with numbers.
 
Upvote 0
Very nice! you are correct it could be either. Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,214,913
Messages
6,122,207
Members
449,074
Latest member
cancansova

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