Hide/Unhide Tabs that Contain a Word.

Robert_Conklin

Board Regular
Joined
Jun 19, 2017
Messages
173
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Good evening, I have the below sub that is set to hide all Tabs with a name that ends in h. I would like to modify it to use the same button to Hide/Unhide all tabs that contain a word instead of ends with a letter.

VBA Code:
Option Explicit


'Set tab naming convention to hide & unhide
Const TABNAME As String = "-h"


Sub Hide_Named_Sheets()
'Hide all sheets that end with -h

Dim ws As Object 'Use object instead of worksheet for Chartsheets

  'Hide sheets with sheet name ending in -h
  For Each ws In ActiveWorkbook.Sheets
    If Right(ws.Name, 2) = TABNAME Then
      ws.Visible = xlSheetHidden
    End If
  Next ws

End Sub

Any help would be greatly appreciated!!
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Change "WORD" to your word !!
VBA Code:
Sub Hide_Named_Sheets()
'Hide all sheets that end with -h
Dim ws As Object 'Use object instead of worksheet for Chartsheets
  For Each ws In Sheets
    If InStr(ws.Name, "WORD") Then
      ws.Visible = xlSheetHidden
    End If
  Next ws
End Sub
 
Upvote 0
Solution
Change "WORD" to your word !!
VBA Code:
Sub Hide_Named_Sheets()
'Hide all sheets that end with -h
Dim ws As Object 'Use object instead of worksheet for Chartsheets
  For Each ws In Sheets
    If InStr(ws.Name, "WORD") Then
      ws.Visible = xlSheetHidden
    End If
  Next ws
End Sub
That worked. I added the "Not ws.Visible" that we applied in the other code you helped me with and now the same button hides and unhides the worksheets. You're the best!
 
Upvote 0
VBA Code:
Sub Hide_Named_Sheets()
Dim ws As worksheet
  For Each ws In workSheets
      ws.Visible = Not ws.Visible
  Next ws
End Sub
 
Upvote 0
VBA Code:
Option Explicit


'Set tab naming convention to hide & unhide
Const TABNAME As String = "WORD"

Sub Hide_Unhide_Named_Sheets()
'Hide all sheets that contain a specific word
Dim ws As Object 'Use object instead of worksheet for Chartsheets
  For Each ws In Sheets
    If InStr(ws.Name, "WORD") Then 'Specify the word you want to use.
      [COLOR=rgb(247, 218, 100)]ws.Visible = Not ws.Visible[/COLOR]
    End If
  Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,428
Members
448,961
Latest member
nzskater

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