Solved

How to record a custom footer Macro in Excel2010?

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

Accepted Solution

by:
Rgonzo1971 earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

763 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

12 Experts available now in Live!

Get 1:1 Help Now