VBA copy Tab info to Header - Output shows as 'Tab]'

GD03

New Member
Joined
Jan 27, 2024
Messages
7
Office Version
  1. 365
Platform
  1. Windows
I've got the following VBA in a macro

It should copy the tab name from my excel worksheet and paste it into a string on the left header
It should also paste 'page x of x Pages' at the bottom of each page

For both of these the text copies OK but the Tab bit shows as 'TAB]' and the 'Page &[Page] of &[Pages]' shows as written here until I open and click on the header manually then it does what I want it to
How can I get the MACRO to do the open/close bit for me .... or to just do it properly in the first place?


VBA Code:
With ActiveSheet.PageSetup

.LeftHeader = "Station Workings" & vbCr & "&[Tab]" & vbCr

.RightHeader = "(Re-formatted by on " & Format(Now(), "D MMM YY)")

.CenterFooter = "Page &[Page] of &[Pages]" & vbCr

End With
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try this macro:
VBA Code:
Public Sub Page_Setup()

    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .LeftHeader = "Station Workings" & vbLf & "&A"
        .RightHeader = "(Reformatted by " & Environ("USERNAME") & " on &D)"
        .CenterFooter = "Page &P of &N"
    End With
    Application.PrintCommunication = True
    
End Sub
 
Upvote 0
Try this macro:
VBA Code:
Public Sub Page_Setup()

    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .LeftHeader = "Station Workings" & vbLf & "&A"
        .RightHeader = "(Reformatted by " & Environ("USERNAME") & " on &D)"
        .CenterFooter = "Page &P of &N"
    End With
    Application.PrintCommunication = True
   
End Sub
Thanks @John_w

Worked perfectly when the header starts off blank but if there's already something in the header (the .leftHeader in this case) it won't replace that text.
The other two which start off blank work fine with the above, cheers :)


Lee
 
Upvote 0
I get the same issue. This seems to fix it:

VBA Code:
Public Sub Page_Setup()

    With ActiveSheet.PageSetup
        .LeftHeader = ""
        .CenterHeader = ""
        .RightHeader = ""
        .LeftFooter = ""
        .CenterFooter = ""
        .RightFooter = ""
    End With
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .LeftHeader = "Station Workings" & vbLf & "&A"
        .RightHeader = "(Reformatted by " & Environ("USERNAME") & " on &D)"
        .CenterFooter = "Page &P of &N"
    End With
    Application.PrintCommunication = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,742
Messages
6,174,243
Members
452,553
Latest member
red83

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