+ Reply to Thread
Results 1 to 5 of 5

VBA for different footers on different sheets in one workbook

Hybrid View

  1. #1
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    VBA for different footers on different sheets in one workbook

    Hi All,

    I was wondering if anyone could offer some assistance on this problem:
    I have one workbook.
    The workbook has 20 sheets.
    The VBA is placed in the "WorkBork" section.
    I only want the VBA to activate on the specific sheets outlined in the code.
    The VBA is set to display a center footer with the text in 12 point Verdana
    The text displayed is the active sheet name then "My text here" then the value of cell B2 (which is a date)
    All the other sheets not listed have specific static headers.
    The code works fine when just individual sheets are printed.

    When the entire workbook is printed however, the VBA footer from Sheet1 is displayed across all the other sheets footers listed in the code instead of the individual sheet's footer.

    Please Help!
    Thanks!

    Sub Workbook_BeforePrint(Cancel As Boolean)
    
    With Sheet1.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      ActiveSheet.Name & " My text here " & Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet5.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      ActiveSheet.Name & " My text here " & Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet4.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      ActiveSheet.Name & " My text here " & Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet6.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      ActiveSheet.Name & " My text here " & Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet8.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      ActiveSheet.Name & " My text here " & Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet7.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      ActiveSheet.Name & " My text here " & Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet9.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      ActiveSheet.Name & " My text here " & Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet10.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      ActiveSheet.Name & " My text here " & Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet11.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      ActiveSheet.Name & " My text here " & Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet12.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      ActiveSheet.Name & " My text here " & Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    End Sub
    Last edited by Pierce Quality; 10-21-2013 at 02:33 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,873

    Re: VBA for different footers on different sheets in one workbook

    In each case, change

    Range("B2").Text
    to

    .Range("B2").Text
    That is, add a dot in front, which means "Use the Range for the sheet specified in the With statement." Otherwise you are using the range in the active sheet throughout.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    Re: VBA for different footers on different sheets in one workbook

    Quote Originally Posted by 6StringJazzer View Post
    In each case, change

    Range("B2").Text
    to

    .Range("B2").Text
    That is, add a dot in front, which means "Use the Range for the sheet specified in the With statement." Otherwise you are using the range in the active sheet throughout.
    Thanks 6StringJazzer,
    When I just added the dot I got a 'run time error 438'.
    I ended up changing the activesheet.name portion to the specfic sheet number, then added the specific sheet number to the .Range portion and it seems to work perfectly now.
    Thanks!

    Heres the end code:

    Sub Workbook_BeforePrint(Cancel As Boolean)
    
    With Sheet1.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      Sheet1.Name & " My text here " & Sheet1.Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet5.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      Sheet5.Name & " My text here " & Sheet5.Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet4.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      Sheet4.Name & " My text here " & Sheet4.Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet6.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      Sheet6.Name & " My text here " & Sheet6.Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet8.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      Sheet8.Name & " My text here " & Sheet8.Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet7.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      Sheet7.Name & " My text here " & Sheet7.Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet9.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      Sheet9.Name & " My text here " & Sheet9.Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet10.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      Sheet10.Name & " My text here " & Sheet10.Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet11.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      Sheet11.Name & " My text here " & Sheet11.Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    With Sheet12.PageSetup
    .CenterFooter = _
      "&12&""Verdana""" & _
      Sheet12.Name & " My text here " & Sheet12.Range("B2").Text
      B2 = Format(Date, " DD MMMM ")
    End With
    
    End Sub

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,873

    Re: VBA for different footers on different sheets in one workbook

    Oh, sorry, I didn't notice that your With statements also included .PageSetup rather than just the sheet. Glad that I was able to help.

  5. #5
    Forum Contributor Pierce Quality's Avatar
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    221

    Re: VBA for different footers on different sheets in one workbook

    Quote Originally Posted by 6StringJazzer View Post
    Oh, sorry, I didn't notice that your With statements also included .PageSetup rather than just the sheet. Glad that I was able to help.
    Thanks, appreciate the help!

+ 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. Replies: 3
    Last Post: 11-29-2012, 03:38 PM
  2. Replies: 0
    Last Post: 11-29-2012, 06:37 AM
  3. Headers and footers on all sheets except the 1st sheet
    By megaman1982 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-10-2007, 08:48 AM
  4. Generating Footers for Multiple sheets in Workbook
    By Ron in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-09-2006, 12:35 PM
  5. [SOLVED] how to add footers to ALL sheets in a workbook at once?
    By KikiMarie in forum Excel General
    Replies: 2
    Last Post: 10-07-2005, 05:05 PM

Tags for this Thread

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