VBA Code for different headers on first page from other pages of same worksheet

Rewound

New Member
Joined
May 12, 2016
Messages
1
Hi All,

First time poster so apologies if this isn't right formatting.

I have seen this question be asked a lot, but having tried all the variety of options, none of them seem to be working.

I have a worksheet which, depending on the value of 1 cell I want to print either 1 or 2 pages - have added this bit, its working, all good.

My issue is, that when 2 pages are selected I need a slightly different header on each of the pages (because we duplex print) which refer to specific cells . Clearly it is possible in Excel 2010, because for standard options (date/pages etc) it's there. But I can't seem to work this out either than by recording and editing macros, or from searching t'internet. - my VBA knowledge is limited!

Neither code works properly but the first is better than the 2nd for what I need. The first code forces the print to be on separate pint commands, which I'd like to avoid and the second not all headers update.

The bit of code that I think needs amendment is this bit (full code for the sub at the end of the post):

Case Else
ActiveSheet.PageSetup.RightHeader = "&b" & "&16" & ActiveSheet.Range("$h$8") & vbLf & "&12" & "Investment"
ActiveSheet.PageSetup.CenterHeader = ""
ActiveSheet.PageSetup.LeftHeader = ""
ActiveSheet.PrintOut From:=1, To:=1
ActiveSheet.PageSetup.LeftHeader = "&b" & "&16" & ActiveSheet.Range("$h$8")
ActiveSheet.PageSetup.CenterHeader = "&b" & "&12" & ActiveSheet.Range("$h$9")
ActiveSheet.PageSetup.RightHeader = ""
ActiveSheet.PrintOut From:=2, To:=2

Also tried (header on first or 2nd page doesn't update/change/stays blank):
Case Else
Application.PrintCommunication = False
With ActiveSheet.PageSetup
.OddAndEvenPagesHeaderFooter = True
.EvenPage.LeftHeader.Text = "&b" & "&16" & ActiveSheet.Range("$h$8")
.EvenPage.CenterHeader.Text = "&b" & "&12" & ActiveSheet.Range("$h$9")
.EvenPage.RightHeader.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = "&b" & "&16" & ActiveSheet.Range("$h$8") & vbLf & "&12" & "Investment"
End With
Application.PrintCommunication = True
ActiveSheet.PrintOut From:=1, To:=2, Preview:=True
With ActiveSheet.PageSetup
.OddAndEvenPagesHeaderFooter = True
.EvenPage.RightHeader.Text = ""
.EvenPage.CenterHeader.Text = ""
.EvenPage.LeftHeader.Text = ""
.FirstPage.LeftHeader.Text = ""
.FirstPage.CenterHeader.Text = ""
.FirstPage.RightHeader.Text = ""
End With
End Select

Any help gratefully received!

Thanks

R

Full code for Macro:


Sub PrintForms()
Dim StartRow As Integer
Dim EndRow As Integer
Dim Msg As String
Dim i As Integer
Dim Partial As String

Sheets(" Form").Activate
StartRow = Range("StartRow")
EndRow = Range("EndRow")
Partial = Range("Partial")

For i = StartRow To EndRow
Range("RowIndex") = i
If Range("Preview") Then
Select Case Range("Partial")
Case Is = "y"
ActiveSheet.PageSetup.RightHeader = "&b" & "&16" & ActiveSheet.Range("$h$8")
ActiveSheet.PageSetup.CenterHeader = ""
ActiveSheet.PageSetup.LeftHeader = ""
ActiveSheet.PrintOut From:=1, To:=1, Preview:=True
Case Is = "Y"
ActiveSheet.PageSetup.RightHeader = "&b" & "&16" & ActiveSheet.Range("$h$8")
ActiveSheet.PageSetup.CenterHeader = ""
ActiveSheet.PageSetup.LeftHeader = ""
ActiveSheet.PrintOut From:=1, To:=1, Preview:=True
Case Else
ActiveSheet.PageSetup.RightHeader = "&b" & "&16" & ActiveSheet.Range("$h$8") & vbLf & "&12" & "Investment"
ActiveSheet.PageSetup.CenterHeader = ""
ActiveSheet.PageSetup.LeftHeader = ""
ActiveSheet.PrintOut From:=1, To:=1, Preview:=True
ActiveSheet.PageSetup.LeftHeader = "&b" & "&16" & ActiveSheet.Range("$h$8")
ActiveSheet.PageSetup.CenterHeader = "&b" & "&12" & ActiveSheet.Range("$h$9")
ActiveSheet.PageSetup.RightHeader = ""
ActiveSheet.PrintOut From:=2, To:=2, Preview:=True
End Select
Else
Select Case Range("Partial")
Case Is = "y"
ActiveSheet.PageSetup.RightHeader = "&b" & "&16" & ActiveSheet.Range("$h$8")
ActiveSheet.PageSetup.CenterHeader = ""
ActiveSheet.PageSetup.LeftHeader = ""
ActiveSheet.PrintOut From:=1, To:=1
Case Is = "Y"
ActiveSheet.PageSetup.RightHeader = "&b" & "&16" & ActiveSheet.Range("$h$8")
ActiveSheet.PageSetup.CenterHeader = ""
ActiveSheet.PageSetup.LeftHeader = ""
ActiveSheet.PrintOut From:=1, To:=1
Case Else
ActiveSheet.PageSetup.RightHeader = "&b" & "&16" & ActiveSheet.Range("$h$8") & vbLf & "&12" & "Investment"
ActiveSheet.PageSetup.CenterHeader = ""
ActiveSheet.PageSetup.LeftHeader = ""
ActiveSheet.PrintOut From:=1, To:=1
ActiveSheet.PageSetup.LeftHeader = "&b" & "&16" & ActiveSheet.Range("$h$8")
ActiveSheet.PageSetup.CenterHeader = "&b" & "&12" & ActiveSheet.Range("$h$9")
ActiveSheet.PageSetup.RightHeader = ""
ActiveSheet.PrintOut From:=2, To:=2 End Select
End If
Next i
End Sub

 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Forum statistics

Threads
1,217,320
Messages
6,135,870
Members
449,966
Latest member
Bradbu

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