Share via

Dynamic Header / Footer

Anonymous
2011-10-06T15:06:44+00:00

Is there a way to reference a cell in a worksheet so it populates in the header or footer?

I have a number of worksheets for different reporting divisions and would like cell B2 to be a portion of the Header.

Sheet 1, B2 = Division of AAAAAAA

Sheet 2, B2 = Division of BBBBBBB

Sheet 3, B2 = Division of CCCCCCC

Sheet 4, B2 = Division of DDDDDDD

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2011-10-06T19:22:09+00:00

So two ways one you want the page setup to be done beforehand

Select Menu insert/Module In the new window, copy this :

Option explicit '  to avoid wrong variable names, this forces you to DECLARE all the variables.comments, behind coded line have to be precedeed with a simple quote like ' ' the name of the sub need to be given in own word

Sub addMyHeaders

Dim wk As Worksheet

    For Each wk In ThisWorkbook.Worksheets    

  wk.PageSetup.CenterHeader = "Division of " & wk.Range("B2").Value

Next wk

End sub

END OF DATA TO BE COPIED  

before executing a macro, thing that ther is no undo in XL macro

Press the F5 function key to run this or press F8 to execute line by line. two Before each print, the page set needt to be corrected

Press ALT F11 double click the " thisworkbook" object in the project explorer add this

Private Sub Workbook_BeforePrint(Cancel As Boolean)

Dim wk As Worksheet

For Each wk In ThisWorkbook.Worksheets

    wk.PageSetup.CenterHeader = "Division of " & wk.Range("B2").Value

 Next wk

End Sub

You can also read the question Import csv data into Excel 2007 template   where my last post give some more info

 Regards JY

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Anonymous
    2011-10-06T17:16:57+00:00

    The sheets are named numerically for an update process that is run monthly.

    Each sheet has the Division and for that matter the month for which the update took place in cells on the worksheet.

    I was hoping that there could be code written in the header to capture the contents of a cell, much like the "&[Tab]" or &[Page] function that currently exists.  Something like &[Cell].[B1] ...

    Any thoughts on this?

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2011-10-06T19:02:14+00:00

    I would appreciate the instruction.

    Thanks

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2011-10-06T17:27:31+00:00

    Hi,

    Yes, if indeed all sheets do need the the same page set-up,

    you can can use my previous post.

    Do you need instrction on how to use it ?

    JY

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2011-10-06T15:42:45+00:00

    Hi,

    If you name the sheets with the Division Name, you can use it in the header sections..

    Or by macro

    Dim wk As Worksheet

    For Each wk In ThisWorkbook.Worksheets

        wk.PageSetup.CenterHeader = "Division of " & wk.Range("B2").Value

    Next wk

    Regards

    JY

    Was this answer helpful?

    0 comments No comments