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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
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"
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,272
Members
449,075
Latest member
staticfluids

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