Consulting

Results 1 to 8 of 8

Thread: Cell Value in Header

  1. #1
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location

    Cell Value in Header

    Stuck my foot in it and now feel obligated to answer a newsgroup question.

    So... help me out, guys. How is this accomplished?

    Let's assume I want A1's value to appear in my header.
    ~Anne Troy

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Anne,

    This is a workbookbeforePrint event that adds the cell value from one sheet into each worksheets header.
    Option Explicit 
    Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Dim WS As Worksheet
    For Each WS In Worksheets
    WS.PageSetup.LeftHeader = Worksheets("Sheet1").Range("A3").Value
    Next WS
    End Sub
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Anne,

    I have no idea if this can be done without code, so maybe I'll learn something here...

    VBA ways:

    Sub AddHeader()
    'Add A1 from active sheet to active sheet's header
    With ActiveSheet.PageSetup
        .LeftHeader = Range("A1").Value
    End With
    End Sub
    
    Sub AddHeaderToAll_1()
    'Add A1 from each sheet to header
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.PageSetup.LeftHeader = ws.Range("A1").Value
    Next ws
    End Sub
    
    Sub AddHeaderToAll_2()
    'Add A1 from active sheet to each sheets's header
    Dim ws As Worksheet
    For Each ws In ActiveWorkbook.Worksheets
        ws.PageSetup.LeftHeader = ActiveSheet.Range("A1").Value
    Next ws
    End Sub
    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Any reason this wouldn't work Ken?



    Option Explicit
    'Add A1 from active sheet to each sheets's header
    Private Sub Workbook_BeforePrint(Cancel As Boolean)
    ActiveSheet.PageSetup.LeftHeader = ActiveSheet.Range("A1").Value
    End Sub


    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Not at all. It just runs every time you print, mine runs once. If the value of the range is always changing and needs updating frequently, then yours would be the right way to go. If its pretty stagnant, then mine saves that extra milisecond that no one would notice anyway!

    Just different ways to skin a cat, really...
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    That makes sense, Thanks Ken..
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    VBAX Expert
    Joined
    Feb 2005
    Posts
    929
    Location
    Quote Originally Posted by kpuls
    Hi Anne,

    I have no idea if this can be done without code, so maybe I'll learn something here...
    I have poked at this several times and concluded each time that the only reliable method was to use code.

    There are lots of things re the management of headers and footers that could be improved
    "It's not just the due date that's important, it's also the do date" [MWE]

    When your problem has been resolved, mark the thread SOLVED by clicking on the Thread Tools dropdown menu at the top of the thread.

  8. #8
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Well then...

    I guess some variants of these should make it into the KB then.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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