Solved

How to record a custom footer Macro in Excel2010?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now