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.
 

Some videos you may like

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

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
 

Watch MrExcel Video

Forum statistics

Threads
1,122,207
Messages
5,594,841
Members
413,944
Latest member
3xc3ln00b

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