Solved

How to record a custom footer Macro in Excel2010?

Posted on 2013-01-30
3
425 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
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…

831 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