Run-time error '1004': Select method of Worksheet class failed. Help :)

Mr Retirement

New Member
Joined
Nov 12, 2016
Messages
46
I'm trying to loop through my sheets and when I get to the 8th sheet, it has this error:

"Run-time error '1004': Select method of Worksheet class failed."


Anyone see my mistake? It works great through the first 7 sheets, not sure why it wont continue.


My Code is:

Code:
Sub RefreshAllWorksheets()
Dim Count, i As Long
 i = 1
 Count = Sheets.Count
    Do While i < Count
     Sheets(i).Select
     
     Sheets(i).Unprotect
     
     Call HypMenuVRefresh
    
     Sheets(i).Cells(1, 12).Select
        ActiveCell.Interior.Color = RGB(255, 255, 255)
     
     Sheets(i).Protect
     
     i = i + 1
    Loop
End Sub

Thanks!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I think it was caused because I had a Sheet hidden. Any tips to edit my code that it will skip hidden sheets and continue?
 
Upvote 0
My code still wouldn't go past the hidden tab. Am I applying your thought correctly?

Code:
Sub RefreshAllWorksheets()
Dim Count, i As Long
 i = 1

 Count = Sheets.Count
    Do While i < Count

    If Sheets(i).Visible = xlSheetVisible Then

     Sheets(i).Select
     
     Sheets(i).Unprotect
     
     Call HypMenuVRefresh
    
     Sheets(i).Cells(1, 12).Select
        ActiveCell.Interior.Color = RGB(255, 255, 255)
     
     Sheets(i).Protect
     
     i = i + 1

     End If

    Loop

End Sub
 
Upvote 0
Not quite. The i = i + 1 should go outside the If block because you always want to increment the loop index.

And shouldn't the loop be Do While i <= Count? Otherwise it doesn't loop to the last sheet. Or just use a For Next loop instead.
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,377
Members
448,888
Latest member
Arle8907

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