+ Reply to Thread
Results 1 to 2 of 2

Reference a Cell in a Print Header/Footer

Hybrid View

RobHam Reference a Cell in a Print... 11-12-2011, 10:11 AM
RobHam Re: Reference a Cell in a... 11-13-2011, 06:06 AM
  1. #1
    Registered User
    Join Date
    11-12-2011
    Location
    Swansea, Wales
    MS-Off Ver
    Excel 2003
    Posts
    2

    Reference a Cell in a Print Header/Footer

    I have a spreadsheet that contains both Sheets and Charts and I want to Reference the contents of a cell in the header/footer of both types using VB.

    The following Micosoft web site contains a nice simple piece of code that I would like to use if possible - uses the Workbook_BeforePrint method to add the cell contents to the header/footer before printing :-

    http://support.microsoft.com/kb/273028

    The following VB code works when printing a Sheet but not with a Chart.

    Private Sub Workbook_BeforePrint(Cancel As Boolean)
       ActiveSheet.PageSetup.LeftFooter = Sheet1.Range("a1").Value
    End Sub
    I can make the code work with a Chart as follows

    Private Sub Workbook_BeforePrint(Cancel As Boolean)
       ActiveChart.PageSetup.LeftFooter = Sheet1.Range("a1").Value
    End Sub
    My problem - if I combine the two I get an error because if the Sheet is active then the Chart is not, and the reverse of this, the Sheet/Chart that is not active generates the error in the code.

    Private Sub Workbook_BeforePrint(Cancel As Boolean)
       ActiveSheet.PageSetup.LeftFooter = Sheet1.Range("a1").Value
       ActiveChart.PageSetup.LeftFooter = Sheet1.Range("a1").Value
    End Sub
    The ideal solution would be for a simple modification to the above code but I seem to be stuck hence any help would be appreciated.

    Robert
    Last edited by RobHam; 11-13-2011 at 07:31 AM.

  2. #2
    Registered User
    Join Date
    11-12-2011
    Location
    Swansea, Wales
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Reference a Cell in a Print Header/Footer

    Following up my own post - I have come up with a solution (after searching through the internet again).

    The following code does now seem to modify both a Sheets header and also a Charts header. I have tested it with both Tab Charts and Embedded Charts. The code has been expanded slightly to include text formatting and to print the Ampersand character correctly (I need this).

    If Not ActiveChart Is Nothing Then
        ActiveChart.PageSetup.LeftHeader = "&""Arial,Regular""&10" & Replace(Sheet1.Range("a1").Value, "&", "&&")
    Else
        ActiveSheet.PageSetup.LeftHeader = "&""Arial,Regular""&10" & Replace(Sheet1.Range("a1").Value, "&", "&&")
    End If

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1