hide sheets based on activate sheet

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
397
Office Version
  1. 2016
Platform
  1. Windows
hello
I try making macro by hide specific sheets based on activate sheet. so if the sheet("data") is activate then just hide sh1,2,3 and show the rest of sheets sh4,5,6 and if the sheet("main") is activate then show sheets sh1,2,3 and hide sh4,5,6.
this is my trying but not work
VBA Code:
Sub vv()
Dim ws As Worksheet
Dim sh, sh1 As Variant
sh = Array("sh1", "sh2", "sh3")
sh1 = Array("sh4", "sh5", "sh6")
For Each ws In Worksheets
   If Sheets("data").Activate Then
      sh.Visible = False
   End If

If Sheets("main").Activate Then
      sh1.Visible = False
         End If
Next ws
End Sub
 

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.
Hello,

Your best bet here would be to add some code into the worksheet module and use code like this:
VBA Code:
Private Sub Worksheet_Activate()
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    If ws.Name = "Sheet3" Or ws.Name = "Sheet4" Or ws.Name = "Sheet5" Then
        ws.Visible = xlSheetHidden
    Else
        ws.Visible = xlSheetVisible
    End If

Next

End Sub

Add this code to both the "data" sheet module, and "main" sheet module, then edit the sheet names for you want visible.
 
Upvote 0
great ! can I put into array instead of using or every time . I try this
VBA Code:
If ws.Name = Array("sh4", "sh5", "sh6")
but doesn't work
 
Upvote 0
It's a tad bit more complicated if you'd like to use an array. Without creating a function, I would try something like this:
VBA Code:
Private Sub Worksheet_Activate()
Dim ws As Worksheet
Dim i As Integer
Dim arr As Variant
Dim SheetList As String

arr = Array("Sheet3", "Sheet4", "Sheet5")

    For i = LBound(arr) To UBound(arr)
        SheetList = SheetList & "," & arr(i)
    Next i
    
SheetList = Mid(SheetList, 2, 10000)

For Each ws In ActiveWorkbook.Worksheets

     If InStr(SheetList, ws.Name) Then
         ws.Visible = xlSheetHidden
     Else
         ws.Visible = xlSheetVisible
     End If

Next

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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