azexceluser

New Member
Joined
Jan 14, 2016
Messages
2
I am using the following code to delete the header from each worksheet in a large Excel workbook. The code is running fine until it gets to one specific worksheet, then I receive the following error message: "Run-time error '1004': Select method of worksheet class failed". I have inserted this code into other workbooks as a test, and I do not get the error, so it is something specific about this one worksheet within this one particular workbook. I have also tried deleting the code, doing a "save as" on the workbook and starting over, but the error continues to occur. Following is the code I am using, I have changed the problem area to red:

Rich (BB code):
Private Sub CommandButton1_Click()

    'Variable Declaration
        Dim cnt As Integer
        Dim i As Integer
                  
    'Assign Variables
        cnt = ActiveWorkbook.Worksheets.Count - 1
        
    'Remove Headers
        Application.ScreenUpdating = False
        For i = 2 To cnt
            Worksheets(i).Select            
            With ActiveSheet.PageSetup
                .LeftHeader = ""
                .CenterHeader = ""
                .RightHeader = ""
            End With
        Next i
        Application.ScreenUpdating = True
                        
End Sub

Any ideas or suggestions would be appreciated!
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Sounds like its a hidden sheet. You can choose to handle it by not activating the hidden sheet:

Code:
    For i = 2 To cnt
        If Worksheets(i).Visible = xlSheetVisible Then
            Worksheets(i).Select
            ' Any other code to run only if sheet is not a hidden sheet
        End If
        ' Code to run for all sheets
    Next i

Or you can choose to change the visible state temporarily and then restore it back:
Code:
    For i = 2 To cnt
        iShState = Worksheets(i).Visible
        Worksheets(i).Visible = xlSheetVisible
        Worksheets(i).Select
        ' Remaining code to run for the sheet
        Worksheets(i).Visible = iShState ' Restore state
    Next i
 
Upvote 0
That appears to have been the issue - thank you! I decided to use your second option (change then restore), and it worked perfectly.
 
Upvote 0
i would like to use this code too but it removes the headers in the NEXT sheet. How do i get the code to remove headers & footers in the active sheet?
Thanks!
 
Upvote 0

Forum statistics

Threads
1,216,267
Messages
6,129,792
Members
449,535
Latest member
Piaskun

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