Different Header on Each Page

NEM25

New Member
Joined
Mar 24, 2009
Messages
3
I have a spreadsheet with warehouse locations in the first column in an Excel worksheet. For each location there can be one or more items. Items are listed in the second column. I need to print out all of this information, with each location on a different page. I have used the following Macro to achieve this:

Sub pagebrk()
col = 1
LastRw = ActiveSheet.UsedRange.Rows.Count
For x = 2 To LastRw
If Cells(x, col) <> Cells(x - 1, col) Then
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Cells(x, col)
End If
Next
End Sub

If possible, I would like to put the location as the header on each page that is printed out. I have tried to find a suitable macro to do this, but so far have failed.

Any advice would be much appreciated.

Cheers,

NEM.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

texasalynn

Well-known Member
Joined
May 19, 2002
Messages
8,458
This will setup a header by a cell range, so maybe this:

ActiveSheet.PageSetup.LeftHeader = _
Format(Worksheets("Sheet2").Range(Cells(x, col)).Value)

put this in the section after "Then"
 

NEM25

New Member
Joined
Mar 24, 2009
Messages
3
Thanks for the suggestion texasalynn. However, when I try it, I get a runitime error '1004' (application-defined or object defined error). This is what the macro now looks like:

Sub PageBreak_and_Header()
col = 1
LastRw = ActiveSheet.UsedRange.Rows.Count
For x = 2 To LastRw
If Cells(x, col) <> Cells(x - 1, col) Then
ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Cells(x, col)
ActiveSheet.PageSetup.LeftHeader = _
Format(Worksheets("Test Page Breaks").Range(Cells(x, col)).Value)
End If
Next
End Sub

Any further suggestions?

Thanks,

NEM.
 

texasalynn

Well-known Member
Joined
May 19, 2002
Messages
8,458
a made a few changes and it seemed to work

Code:
Sub PageBreak_and_Header()
Dim col As Long, x As Long
Dim ws As String
col = 1
LastRw = ActiveSheet.UsedRange.Rows.Count
For x = 2 To LastRw
If Cells(x, col) <> Cells(x - 1, col) Then
    ActiveWindow.SelectedSheets.HPageBreaks.Add Before:=Cells(x, col)
    ws = ActiveSheet.Name
    Sheets(ws).PageSetup.LeftHeader = ActiveSheet.Cells(x, col)
End If
Next
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,158
Messages
5,835,718
Members
430,383
Latest member
Kastore

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
Top