Results 1 to 15 of 15

Thread: Variables in Excel Header

  1. #1

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677

    Variables in Excel Header

    Is there a way to have VB put variables into the Header/Footer of an Excel worksheet? I'd like to be able to have it so that the header contained a variable like an employee name or something. Anyone got any ideas? Thanks, Jeremy
    He who listens well, speaks well.

  2. #2
    Addicted Member
    Join Date
    Aug 2003
    Location
    houston
    Posts
    185
    Code:
    With ActiveSheet.PageSetup
            
            .CenterHeader = Range("rTitle").Value
            
            
    End With
    this will place the contents of the named range into thecenter header

  3. #3
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Anyone know how to adjust the Font in a header programatically?

    Dave

  4. #4

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    The same way you would any other font object in Excel. I'm not being mean about this. What I am saying is that all object in Excel are basically visible to the Object Model. Example:

    Code:
    ActiveSheet.PageSetup.CenterHeader
    And once you get to an object, you can modify it's attributes/parameters the same way. Example:

    Code:
    objExcelQuote.Selection.NumberFormat = "$#,##0.0000"
    So...if you put both of what I said together, you'll be able to do this, I assure you. Laters, Jeremy
    He who listens well, speaks well.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    This is the most control you can get over the headers or footers in Excel.
    VB Code:
    1. Sub FormatPageHeaderFooter()
    2.  
    3.     With ActiveSheet.PageSetup
    4.         .LeftHeader = "&""Arial,Bold""&12Font color test"
    5.         '.LeftHeader = "&D" 'PRINTS CURRENT DATE
    6.         .CenterHeader = ""
    7.         .RightHeader = ""
    8.         .LeftFooter = ""
    9.         .CenterFooter = ""
    10.         .RightFooter = ""
    11.         .LeftMargin = Application.InchesToPoints(0.75)
    12.         .RightMargin = Application.InchesToPoints(0.75)
    13.         .TopMargin = Application.InchesToPoints(1)
    14.         .BottomMargin = Application.InchesToPoints(1)
    15.         .HeaderMargin = Application.InchesToPoints(0.5)
    16.         .FooterMargin = Application.InchesToPoints(0.5)
    17.         .PrintHeadings = False
    18.         .PrintGridlines = False
    19.         .PrintComments = xlPrintNoComments
    20.         .PrintQuality = -3
    21.         .CenterHorizontally = False
    22.         .CenterVertically = False
    23.         .Orientation = xlPortrait
    24.         .Draft = False
    25.         .PaperSize = xlPaperLetter
    26.         .FirstPageNumber = xlAutomatic
    27.         .Order = xlDownThenOver
    28.         .BlackAndWhite = False
    29.         .Zoom = 100
    30.         .PrintErrors = xlPrintErrorsDisplayed
    31.     End With
    32.     ActiveWindow.SelectedSheets.PrintPreview
    33.    
    34. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Jeremy,

    Code:
    ActiveSheet.PageSetup.CenterHeader
    Does not return an object with which to control the font or even get a Font object.

    The "CenterHeader" part is itself an object of type String, and Strings do not return handles to objects like the Font object.

    Dave

    Code:
    ActiveSheet.PageSetup.CenterHeader
    And once you get to an object, you can modify it's attributes/parameters the same way. Example:

    Code:
    objExcelQuote.Selection.NumberFormat = "$#,##0.0000"
    So...if you put both of what I said together, you'll be able to do this, I assure you. Laters, Jeremy [/B]

  7. #7
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Thanks, robdog, this definately works! I assume you got this by recording a macro and looking at the code? I should really learn to do that too...

    .LeftHeader = "&""Arial,Bold""&12Font color test"
    Dave

  8. #8

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Originally posted by Dave Sell
    Jeremy,

    Code:
    ActiveSheet.PageSetup.CenterHeader
    Does not return an object with which to control the font or even get a Font object.

    The "CenterHeader" part is itself an object of type String, and Strings do not return handles to objects like the Font object.

    Dave
    d00d...that was an example of how a user would access object and attributes. You are correct on what you said but in my example, it was to show that you can access object/attributes using that methodology. Thanks, Jeremy
    He who listens well, speaks well.

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    More info on page headers/footers.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Great info, thanks!

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    No prob.

    btw, I did grab the info from recording a macro.
    Its really easy. You should try it.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    OK this is really shameful, but ... I DID try it. I couldnt figure out how to record a macro... I know it makes me sound stupid but I just havent tackled it yet.

    Maybe you have some quick pointers? I had the macro recording window opened from Excel, and I could see how to play a macro - but I didnt see a way to record one.

    Thanks,

    Dave

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Tools > Macro > Record New Macros... >
    Then name the macro as you wish under the Macro Name:
    Then store in this workbook, usually unless you want to store
    your macro in the personal macros workbook or another
    workbook.
    Then, add a description if you want.

    Click ok and you are recording.
    When you are finished, click on the stop button of the macro
    toolbar that will popup when you start recording.

    Then you can view the code by going to the VBA Editor.

    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Thanks, I will definately try it this week!

  15. #15
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961
    Got it to work, that'll be worth gold!

Posting Permissions

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



Click Here to Expand Forum to Full Width