excel vba change header at each page

planetexcel

New Member
Joined
Feb 23, 2006
Messages
25
Is there a way to pass the top left cell value of each page to the header?

Thanks in advance,

Stelios
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try this:

Sub try
dim x as integer
dim hdr as string
x=1
do until x=worksheets.count+1
sheets(x).activate
hdr=cells(1,1).value
With ActiveSheet.PageSetup
.CenterHeader = hdr
End With
x=x+1
loop
end sub
 
Upvote 0
Dear friend i am talking about printing one worksheet with different header in each page. Which it will be each page top left cell.
 
Upvote 0
Hello.

If you paste the macro that I have written below into your workbook, then run it, you shoudl I think get what you want.

i will go through the process:

Code:
Sub try
dim x as integer    [COLOR=seagreen] 'sets up a variable to go through each worksheet[/COLOR]
dim hdr as string    [COLOR=seagreen]'sets up a variable to hold the title you want in the header
[/COLOR]x=1                    [COLOR=seagreen] 'starts with first sheet in your workbook[/COLOR]
do until x=worksheets.count+1          [COLOR=seagreen]'starts a loop for all of your sheets[/COLOR]
sheets(x).activate       [COLOR=seagreen]'activates sheet x (1st sheet then looping)
[/COLOR]hdr=cells(1,1).value    [COLOR=seagreen] 'picks up the value you have in the top left of that sheet and assigns it to variable hdr
[/COLOR]With ActiveSheet.PageSetup
.CenterHeader = hdr      [COLOR=seagreen]'sets the header to whatever is assigned to[/COLOR] [COLOR=seagreen]variable hdr[/COLOR]
End With
x=x+1                   [COLOR=seagreen]' adds one to x so that it can look at the next sheet in your workbook
[/COLOR]loop                     [COLOR=seagreen]'loops to go through the process again with next sheet[/COLOR].
end sub

I have tested this and it definitely looks at whatever is in cell A1 of each sheet and then puts that into the header.
 
Upvote 0
What I was looking and after hard searching was the following..


Sub Prt()
'The first page
ActiveSheet.PageSetup.RightHeader = "Department : " & Cells(ActiveSheet.HPageBreaks(1).Location.Row - 1, 5).Value
ActiveSheet.PrintOut From:=1, To:=1
counter = 1
'to get the other pages and manipulate them
For Each hb In ActiveSheet.HPageBreaks
counter = counter + 1
ActiveSheet.PageSetup.RightHeader = "Department : " & Cells(hb.Location.Row, 5).Value
ActiveSheet.PrintOut From:=counter, To:=counter
Next hb
End Sub

Thanks for the try.
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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