fixing error run time error 1004 to hide specific sheets

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hello
i have this code trying hide specific sheets but it gives me error run time error 1004 "method visible of object_ worksheet failed " in this line
VBA Code:
sh.Visible = xlSheetVeryHidden

VBA Code:
Private Sub CommandButton2_Click()
Application.Visible = True
Sheets("micro").Activate
Dim sh As Worksheet

For Each sh In Worksheets(Array("RAW", "Date", "MICC", "REPORT", "LABLE"))
    sh.Visible = xlSheetVeryHidden
Next sh
UserForm1.hide

End Sub
i truly appreciate if anybody help
 
Last edited:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi,
Is the workbook protected?

Dave
 
Upvote 0
in that case, try following update to your code & see if helps

VBA Code:
Private Sub CommandButton2_Click()
    Application.Visible = True
    Dim sh As Worksheet
   
    With ThisWorkbook.Worksheets("micro")
        .Visible = xlSheetVisible
        .Activate
    End With
   
    For Each sh In ThisWorkbook.Worksheets(Array("RAW", "Date", "MICC", "REPORT", "LABLE"))
        sh.Visible = xlSheetVeryHidden
    Next sh
    UserForm1.Hide

End Sub

Dave
 
Upvote 0
thanks so much for your updating but i would understand me why my code doesn't work i would make clear above code works for commandbutton1 but the commandbutton2 doesn't work despite the same code the difference is name of sheet this not work
VBA Code:
Private Sub CommandButton2_Click()
Application.Visible = True
Sheets("raw").Activate
Dim sh As Worksheet
For Each sh In Worksheets(Array("MICRO", "Date", "MICC", "REPORT", "LABLE"))
    sh.Visible = xlSheetVeryHidden
Next sh
UserForm1.hide
End Sub
i hope you can answer me
 
Upvote 0
Your code for CommandButton2 does not include the updates I made

VBA Code:
Private Sub CommandButton2_Click()
    Application.Visible = True
    Dim sh As Worksheet
  
    With ThisWorkbook.Worksheets("raw")
        .Visible = xlSheetVisible
        .Activate
    End With
  
    For Each sh In ThisWorkbook.Worksheets(Array("RAW", "Date", "MICC", "REPORT", "LABLE"))
        sh.Visible = xlSheetVeryHidden
    Next sh
    UserForm1.Hide

End Sub

I can only conclude that the error is possibly due to code trying to hide ALL sheets which is not possible. My update ensures that sheet is visible before the hide code runs

Dave
 
Upvote 0
Solution
thanks dmt32 for all of thing your updating works well
 
Upvote 0

Forum statistics

Threads
1,214,787
Messages
6,121,569
Members
449,038
Latest member
Guest1337

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