Solved

How to record a custom footer Macro in Excel2010?

Posted on 2013-01-30
3
429 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 50

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

713 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