help with error 1004

fry

Active Member
Joined
Apr 25, 2007
Messages
411
I was using the following code to list certain worksheets of certain colours and all was running well until all of a sudden it stopped working and popped up with the following error...

Method 'Visible'of object '_Worksheet' failed

HELLLP anyone :(

Code:
Private Sub CommandButton1_Click()
Dim curWorksheet As Worksheet
For Each curWorksheet In ThisWorkbook.Worksheets
If curWorksheet.Tab.ColorIndex = 5 Then
curWorksheet.Visible = True
Else
curWorksheet.Visible = False
End If
Next
Sheets(1).Visible = True
Sheets(1).Select
End Sub

Private Sub CommandButton2_Click()
Dim curWorksheet As Worksheet
For Each curWorksheet In ThisWorkbook.Worksheets
If curWorksheet.Tab.ColorIndex = 10 Then
curWorksheet.Visible = True
Else
curWorksheet.Visible = False
End If
Next
Sheets(1).Visible = True
Sheets(1).Select
End Sub

Private Sub CommandButton3_Click()
Dim curWorksheet As Worksheet
For Each curWorksheet In ThisWorkbook.Worksheets
curWorksheet.Visible = True
Next
End Sub

Private Sub CommandButton4_Click()
Dim curWorksheet As Worksheet
For Each curWorksheet In ThisWorkbook.Worksheets
If curWorksheet.Tab.ColorIndex = 3 Then
curWorksheet.Visible = True
Else
curWorksheet.Visible = False
End If
Next
Sheets(1).Visible = True
Sheets(1).Select
End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
I forgot to mention that when debugging the following is highlighted...

curWorksheet.Visible = False

I'm stumped cos if I put the code in another blank worksheet it works flawlessly???????? :(
 
Upvote 0
I may have fixed this?

I noticed the workbook was protected and unprotected it after which it seemed to work.

Does anyone know if the protection would cause this error??? :)
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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