Formulas in Excel Footer?

panicbutton

New Member
Joined
Dec 14, 2010
Messages
3
Hey there,

i need some VBA help...
What I am looking for is a footer that is automatically populated with the following information:

Left footer:
<table width="64" border="0" cellpadding="0" cellspacing="0"><tbody><tr style="height: 15pt;" height="20"><td class="xl63" style="height: 15pt; width: 48pt;" width="64" height="20">=Sheet5!AD234</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl63" style="height: 15pt;" height="20">=Sheet1!B45</td> </tr> <tr style="height: 15pt;" height="20"> <td class="xl63" style="height: 15pt;" height="20">=today()</td> </tr> </tbody></table>
center footer:
Page number

Please note, that the workbook as multiple worksheets, but I want this macro only to run over - lets say - Sheet3.

My next question would be, how can we trigger this macro? Is there a way to start if before the user hits 'print', 'save' or ' 'save as pdf'?

thanks a lot for your help!
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
In the Workbook_BeforePrint event.

Hope it helps.

Gary


Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)

If ActiveSheet.Name = "Sheet3" Then

    ActiveSheet.PageSetup.LeftFooter = Worksheets("Sheet5").Range("AD234").Value & " " & Worksheets("Sheet1").Range("B45").Value & " " & Date
    ActiveSheet.PageSetup.CenterFooter = "Page &P"

End If

End Sub
 
Upvote 0
Thanks Gary.
Is there a way to have multiple lines in the left footer below each other instead of only the one as in your macro?

also, can I trigger the Macro automatically when the workbook is saved? I probably should have said so, but printing of the worksheet would rather be the exception.
Thanks!
 
Upvote 0
There is also a Workbook_BeforeSave event. You can put the same code in there.

You can embed the constant vbcrlf (carriage return / line feed) in the string to force linefeeds. Like so:

Code:
"This is a" & vbcrlf & "Test"

The word test would be on a new line.

Gary
 
Upvote 0
Make that just vblf

vbcrlf gives 2 linefeeds.

Here it is modified:

Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)

If ActiveSheet.Name = "Sheet3" Then

    ActiveSheet.PageSetup.LeftFooter = Worksheets("Sheet5").Range("AD234").Value & vbLf & Worksheets("Sheet1").Range("B45").Value & vbLf & Date
    ActiveSheet.PageSetup.CenterFooter = "Page &P"

End If

End Sub

For the "before save" event:

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

If ActiveSheet.Name = "Sheet3" Then

    ActiveSheet.PageSetup.LeftFooter = Worksheets("Sheet5").Range("AD234").Value & vbLf & Worksheets("Sheet1").Range("B45").Value & vbLf & Date
    ActiveSheet.PageSetup.CenterFooter = "Page &P"

End If

End Sub

Gary
 
Upvote 0

Forum statistics

Threads
1,215,332
Messages
6,124,314
Members
449,153
Latest member
JazzSingerNL

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top