Solved

How to record a custom footer Macro in Excel2010?

Posted on 2013-01-30
3
421 Views
Last Modified: 2013-02-03
I want to record a macro in Excel 2010 to put in a custom header/footer.
I have recorded the steps of customizing the footer, then stop recording.
But, after i save the spreadsheet and reopen it again, it does not show  the expected outcome when it is run via the macro.
Is there a bug?
How to record a custom footer?


The code below is what did:

Sub Footer()
'
' Footer Macro
'

'
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .PrintTitleRows = ""
        .PrintTitleColumns = ""
    End With
    Application.PrintCommunication = True
    ActiveSheet.PageSetup.PrintArea = ""
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .LeftHeader = ""
        .CenterHeader = ""
        .RightHeader = ""
        .LeftFooter = "&Z&F"
        .CenterFooter = "&P"
        .RightFooter = "&D&T"
        .LeftMargin = Application.InchesToPoints(0.236220472440945)
        .RightMargin = Application.InchesToPoints(0.236220472440945)
        .TopMargin = Application.InchesToPoints(0.748031496062992)
        .BottomMargin = Application.InchesToPoints(0.748031496062992)
        .HeaderMargin = Application.InchesToPoints(0.31496062992126)
        .FooterMargin = Application.InchesToPoints(0.31496062992126)
        .PrintHeadings = False
        .PrintGridlines = True
        .PrintComments = xlPrintSheetEnd
        .PrintQuality = 600
        .CenterHorizontally = False
        .CenterVertically = False
        .Orientation = xlPortrait
        .Draft = False
        .PaperSize = xlPaperA4
        .FirstPageNumber = xlAutomatic
        .Order = xlOverThenDown
        .BlackAndWhite = False
        .Zoom = 100
        .PrintErrors = xlPrintErrorsDisplayed
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .ScaleWithDocHeaderFooter = True
        .AlignMarginsHeaderFooter = False
        .EvenPage.LeftHeader.Text = ""
        .EvenPage.CenterHeader.Text = ""
        .EvenPage.RightHeader.Text = ""
        .EvenPage.LeftFooter.Text = ""
        .EvenPage.CenterFooter.Text = ""
        .EvenPage.RightFooter.Text = ""
        .FirstPage.LeftHeader.Text = ""
        .FirstPage.CenterHeader.Text = ""
        .FirstPage.RightHeader.Text = ""
        .FirstPage.LeftFooter.Text = ""
        .FirstPage.CenterFooter.Text = ""
        .FirstPage.RightFooter.Text = ""
    End With
    Application.PrintCommunication = True
End Sub

Open in new window

0
Comment
Question by:user2073
3 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 250 total points
ID: 38838554
Hi,

The only necesary lines in your code are here.

Sub Footer()
'
' Footer Macro
'
    With ActiveSheet.PageSetup
        .LeftFooter = "&Z&F"
        .CenterFooter = "&P"
        .RightFooter = "&D&T"
    End With
End Sub

Open in new window


Could you explain what is wrong and what should the outcome be?

Regards
0
 

Author Comment

by:user2073
ID: 38842149
I have tried to record a macro in Excel 2010 to put in a custom footer.

1. Recording the Macro in Developer Ribbon.
2. Go to Page Setup and Custom Header (or Footer)
3. Inserted the Date/Time field in the right. Inserted the Filepath in the left.Inserted the page in the center.
4. Click OK and Closed header/footer
5. Stopped Macro recording.

When the macro is rerun, and viewed in Page Layout, odd codes appear &L in the page center and the date without time appears on the right.
0
 
LVL 14

Assisted Solution

by:Faustulus
Faustulus earned 250 total points
ID: 38846067
Not every action you can execute in Excel can also be recorded as executable code. Headers and footers are difficult animals for Excel because they pertain to formatting pages instead of manipulating numbers. Excel is good at the latter only. I can't tell you why Excel can't record creation of a custom footer but I can assure you that it isn't a bug. There are many other actions that can't be recorded.
As you progress in your knowledge of VBA you will increasingly find that the recorder only gives you hints as to which syntax might be used. In the end your own code will be better than the recorder's. Rgonzo1971's suggestion demonstrates the point. Usually, when the recorder records something it works. But it is rather the rule than the exception that the recorder's code is much more voluminous and complicated than required. So, I suggest you take Rgonzo1971's code and don't fret over not being able to get the recorder's. It wouldn't be nearly as good even if it were available.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question