VBA / Macro that will Deselect the First Worksheet

diceicle

New Member
Joined
Feb 12, 2020
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
I am looking to create a Macro that selects all the sheets within a file - then it deselects the first sheet regardless of what it is called - and applies a Footer message to the remaining sheets.

I have searched but was unsuccessful in finding a solution I can use.
Thanks for reading.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
First sheet can be identified as Sheets(1) also, regardless the name
 
Upvote 0
Hi diceicle,

Welcome to MrExcel!!

As long as the first (furthest left) sheet is not hidden this should do the job:

VBA Code:
Option Explicit
Sub Macro1()

    Dim i As Long
    Dim ws As Worksheet
    
    Application.ScreenUpdating = False
    
    'Starting from 2 excludes the first (furthest left) sheet in the workbook.
    For i = 2 To ThisWorkbook.Sheets.Count
        Set ws = Sheets(i)
        With ws.PageSetup
            .Orientation = xlPortrait 'Or xlLandscape
            .LeftFooter = "This"
            .CenterFooter = "is the"
            .RightFooter = "Footer"
        End With
    Next i
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Solution
@Trebor76 very much appreciated for that.
I managed to get it working one time but hasn't been firing since. Can't for the life of me figure where I'm going wrong.
I put the code in a Module in my Personel workbook. Ran said Macro and.....nothing.
 
Upvote 0
Not too sure but at a guess I'd say the code is running on your personal workbook, not the desired workbook.

See if changing this line of code...

VBA Code:
For i = 2 To ThisWorkbook.Sheets.Count

to this does the job:

VBA Code:
For i = 2 To ActiveWorkbook.Sheets.Count
 
Upvote 0
From my experience answering to this forum, ThisWorkbook does not on Excel 365. It still works on my 2016 though and should work on 2010, but as you know anything is probably 99.9% sure with Excel. My workbook runs for year on 2016 32bit failed to run on 2016 64bit. I need to tweak the code ?
 
Upvote 0
Not too sure but at a guess I'd say the code is running on your personal workbook, not the desired workbook.

See if changing this line of code...

VBA Code:
For i = 2 To ThisWorkbook.Sheets.Count

to this does the job:

VBA Code:
For i = 2 To ActiveWorkbook.Sheets.Count

Very much appreciated. Switching to 'ActiveWorkbook' worked a treat (on my excel 2010). Thanks a million for your help.
 
Upvote 0

Forum statistics

Threads
1,215,063
Messages
6,122,928
Members
449,094
Latest member
teemeren

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