Hide all worksheets that don't contain the string value of the active cell.

USAMax

Well-known Member
Joined
May 31, 2006
Messages
851
Office Version
  1. 365
Platform
  1. Windows
I want two functions, the first will hide every worksheet that does not contain the string value of the current cell. I have a workbook with a list of properties and column 'L' has the address. That address will be a part of the name of at least two other worksheets Comps - ADDRESS and Prop Card - ADDRESS. Only the Main Property Sheet and those two worksheets should be visible.

The second function should unhide all worksheets.

I am sure this will be a loop that will go through every worksheet but I have not done this before.

Thanks,

Dave
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I am sorry, this was not as hard as I thought it would be.

VBA Code:
Sub HideAllWorksheetsWithoutAddress()

    Dim WS_Count As Integer
    Dim I As Integer
    Dim strAddress As String
   
    'GET ADDRESS
    strAddress = Cells(ActiveCell.Row, 12)
    If strAddress = "" Or ActiveCell.Row = 1 Then GoTo CleanExit
   
    WS_Count = ActiveWorkbook.Worksheets.count

    For I = 1 To WS_Count
        If Sheets(I).Name <> "Main Property Sheet" Then
            If InStr(1, UCase(Sheets(I).Name), UCase(strAddress)) = 0 Then
                Sheets(I).Visible = False
            Else
                Sheets(I).Visible = True
            End If
        End If
    Next I

CleanExit:
End Sub


Sub UnhideAllWorksheets()
    Dim WS_Count As Integer
    Dim I As Integer
   
    WS_Count = ActiveWorkbook.Worksheets.count

    For I = 1 To WS_Count
        Sheets(I).Visible = True
    Next I

CleanExit:
End Sub
 
Last edited by a moderator:
Upvote 0
Solution
Thanks for posting your solution. However, for the future, when posting vba code in the forum, please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details. I have added the tags for you this time. 😊
 
Upvote 0

Forum statistics

Threads
1,225,613
Messages
6,186,005
Members
453,334
Latest member
Prakash Jha

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