|
|
|
|
|
|
Excel
|
3 ways to put a cell value in a page header
|
|
Ease of Use
|
Easy
|
Version tested with
|
2003
|
Submitted by:
|
Ken Puls
|
Description:
|
Shows three different way to put a cell value in a header.
|
Discussion:
|
This demonstrates three different macros to put a cell value in the header of a page. Since there is no way to add a formula in Page Setup, a macro must be used to accomplish this task. Each of these macros can be run before printing, and are not really intended for constant updating. If you need to sync the range name before you print every time, you will need to either call one of these macros from a BeforePrint event, or find/write a BeforePrint event to do ths for you. (Ask in the forum for help if you like.) You can also switch this to work for the page Footer by changing "LeftHeader" to "LeftFooter" in the code.
|
Code:
|
instructions for use
|
Option Explicit
Sub AddHeader_CurrentSheetOnly()
With ActiveSheet.PageSetup
.LeftHeader = Range("A1").Value
End With
End Sub
Sub AddHeaderToAll_FromEachSheet()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.PageSetup.LeftHeader = ws.Range("A1").Value
Next ws
End Sub
Sub AddHeaderToAll_FromCurrentSheet()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
ws.PageSetup.LeftHeader = ActiveSheet.Range("A1").Value
Next ws
End Sub
|
How to use:
|
- Copy the desired procedure from the above code (one set from Sub to End Sub).
- In Excel press Alt + F11 to enter the VBE.
- Press Ctrl + R to show the Project Explorer.
- Right-click desired file on left (in bold).
- Choose Insert -> Module.
- Paste code into the right pane.
- Press Alt + Q to close the VBE.
- Save workbook before any other changes.
|
Test the code:
|
- Enter some data in cell A1 (on each sheet if necessary).
- Press Alt + F8 to display the Macro dialog box.
- Choose the name of the macro you copied and click Run.
- Go to File|Page Setup|Header/Footer and verify that your header has been set correctly.
|
Sample File:
|
RngA1toHeader.zip 8.83KB
|
Approved by mdmackillop
|
This entry has been viewed 304 times.
|
|