VBA- Hiding Tabs with error skip

Woodleigh

New Member
Joined
Jun 3, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Having some trouble with Getting this to work. What i am trying to do is have this tabe "New PVA Summary" Always visible, but the rest of the tabs hidden. So if all the tabs are visible, when you open the Workbook, it automatically hide and locks the workbook. This only works though if the tabs are visible, if the tabs are hidden I am getting and error.

I changed my code to have it tab name dependent but it seems i am now running into a "Type mismatch error", can't seem to figure this out. any help would be appreciated. I am sure it something super simple. Below is the code:

Dim WS As Worksheets

For Each WS In ActiveWorkbook.Worksheets
If WS.Name Like "New PVA Summary*" Then
WS.Visible = True
Else
WS.Visible = False
End If
Next WS

ActiveWorkbook.Protect ("123")
End Sub
 

Attachments

  • 1622742886096.png
    1622742886096.png
    63.5 KB · Views: 11

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Welcome to the board, try:
VBA Code:
Sub Macro1()
  
    Dim x As Long
    Const sheetName As String = "Daily TimeTable*"
  
    On Error Resume Next
    For x = 1 To Worksheets.Count
        Sheets(x).Visible = Sheets(x).name Like sheetName
    Next x
    On Error GoTo 0   

    ActiveWorkbook.Protect ("123")

End Sub
 
Last edited:
Upvote 0
While this works in hiding the tab and not providing an error message, This seems to permanently hid the Tabs, and the other macro to unhide the them with the same password provides " incorrect password" error debug message. i have unprotected the worksheet and tried to manually unhide with no luck
 
Upvote 0
How about
VBA Code:
   Dim WS As Worksheet
   
   ActiveWorkbook.Unprotect "123"
   For Each WS In ActiveWorkbook.Worksheets
      If WS.Name Like "Main*" Then
         WS.Visible = True
      Else
         WS.Visible = False
      End If
   Next WS
   
   ActiveWorkbook.Protect ("123")
 
Upvote 0
Solution
How about
VBA Code:
   Dim WS As Worksheet
  
   ActiveWorkbook.Unprotect "123"
   For Each WS In ActiveWorkbook.Worksheets
      If WS.Name Like "Main*" Then
         WS.Visible = True
      Else
         WS.Visible = False
      End If
   Next WS
  
   ActiveWorkbook.Protect ("123")
absolute legend! works perfectly.

Cheers mate, I knew it would be simple!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Some code changes (reconciled against @Fluff's) try:
VBA Code:
Sub Macro1()
   
    Dim x As Long
    Const sheetName As String = "Main*"
   
    ActiveWorkbook.Unprotect "123"
   
    On Error Resume Next
    For x = 1 To Worksheets.Count
        Sheets(x).Visible = Sheets(x).Name Like sheetName
    Next x
    On Error GoTo 0
   
   ActiveWorkbook.Protect "123"
   
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
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