+ Reply to Thread
Results 1 to 6 of 6

VBA to create custom headers for certain worksheets in a workbook

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    09-09-2009
    Location
    Liverpool, England
    MS-Off Ver
    W: 2021 H: 365
    Posts
    940

    VBA to create custom headers for certain worksheets in a workbook

    Hi,

    I'd like to create custom headers for certain worksheets in my workbook. They will need to be a mix of text and a date reference from a cell in the workbook. The worksheets are called:
    • Spend
    • Savings
    • Sector
    • Customer & Supplier
    • MI

    The first text element will be "Performance Report".

    The date (located in a tab called 'Lookup', cell C19, that has a named range of 'Lookup.MonthSelected') is input in the format xx/xx/xxxx. I'd like it to show in the header in the format mmmm yyyy.

    The last text element will be as follows in relation to the list above.
    • "- Spend Analysis"
    • "- Savings Position"
    • "- Sector Analysis"
    • "- Customer & Supplier Analysis"
    • "- MI Position"

    For example, the header for the 'Spend' tab will be 'Performance Report November 2016 (or whatever date is selected in Lookup!C19) - Spend Analysis'

    Thanks in advance,

    Snook
    Last edited by The_Snook; 11-11-2016 at 05:33 AM.

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA to create custom headers for certain worksheets in a workbook

    Hi,

    Try this routine
    Sub setupHeaders()
    Dim sheetNames
    Dim headerText
    Dim n As Long
    sheetNames = Array("Spend", "Savings", "Sector", "Customer & Supplier", "MI")
    headerText = Array("Spend Analysis", "Savings Position", "Sector Analysis", "Customer & Supplier Analysis", "MI Position")
    
    For n = LBound(sheetNames) To UBound(sheetNames)
        Sheets(sheetNames(n)).PageSetup.CenterHeader = "Performance Report " & _
            Format$(Sheets("Lookup").Range("Lookup.MonthSelected").Value, "mmmm yyyy") & " - " & headerText(n)
    Next n
    End Sub
    You didn't mention which part of the header to use so I put in the middle.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Valued Forum Contributor
    Join Date
    09-09-2009
    Location
    Liverpool, England
    MS-Off Ver
    W: 2021 H: 365
    Posts
    940

    Re: VBA to create custom headers for certain worksheets in a workbook

    Cheers xlnitwit, it works a treat!

    I've merged it with an event driven macro I use to time stamp the footer with the last saved detail. How can I tweak it so that the font (in the header only) is set to bold Arial size 10?

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
      Cancel As Boolean)
      
    Dim sht As Worksheet
    Dim sheetNames
    Dim headerText
    Dim n As Long
    
    sheetNames = Array("Spend", "Savings", "Sector", "Customer & Supplier", "MI")
    headerText = Array("Spend Analysis", "Savings Position", "Sector Analysis", "Customer && Supplier Analysis", "MI Position")
        
    
        For Each sht In Sheets
            sht.PageSetup.LeftFooter = _
              "Last Saved: " & Format(Date, "mmmm d, yyyy") & " " & Time
        Next
        
        For n = LBound(sheetNames) To UBound(sheetNames)
        Sheets(sheetNames(n)).PageSetup.CenterHeader = "Technology Pillar Performance Report " & _
            Format$(Sheets("Lookup").Range("Lookup.MISOMonthSelected").Value, "mmmm yyyy") & " - " & headerText(n)
        Next n
        
    End Sub

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA to create custom headers for certain worksheets in a workbook

    Just change the first part to
    Sheets(sheetNames(n)).PageSetup.CenterHeader = "&""Arial,Bold""&10 Technology Pillar Performance Report " & _

  5. #5
    Valued Forum Contributor
    Join Date
    09-09-2009
    Location
    Liverpool, England
    MS-Off Ver
    W: 2021 H: 365
    Posts
    940

    Re: VBA to create custom headers for certain worksheets in a workbook

    Magical!

    Still can't give you rep. I'll pop back at a later date.....

    Popped back, rep given.
    Last edited by The_Snook; 11-30-2016 at 04:11 AM.

  6. #6
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA to create custom headers for certain worksheets in a workbook

    Thank you!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Create pivot Data from different Workbook With Different Column Headers.
    By danallamas in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 07-26-2016, 08:21 AM
  2. Replies: 4
    Last Post: 02-08-2014, 02:31 PM
  3. How to create new workbook with custom ribbon in VBA
    By MichalJB in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-25-2013, 02:52 PM
  4. Create headers in workbook based on cell references.
    By Saggy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-15-2013, 11:04 AM
  5. Replies: 3
    Last Post: 11-29-2012, 03:38 PM
  6. [SOLVED] How do I change headers for all worksheets in a workbook?
    By frankfine in forum Excel General
    Replies: 7
    Last Post: 08-11-2005, 12:05 PM
  7. [SOLVED] Create a custom footer for all Excel worksheets at one time...
    By bigcat in forum Excel General
    Replies: 2
    Last Post: 03-30-2005, 08:06 PM

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