Is there a way to use Excel-VBA code in order to make a picture object in a sheet, so as to insert it as a footer image. I have tried to do so by creating a chart object and pasting it in a picture-format, and then exporting the chart to an image file and setting the image as the footer. Is there a better way to insert a picture object as a footer image, and if so, how do I do it?
asked Mar 29, 2012 at 13:47
4
I started the macro recorder. I clicked Page Setup
then Header/Footer
then Custom Footer
. I clicked the centre section and then Format Picture
(button with image of sun over mountains). I browsed for an image and clicked Insert
. «&[Picture]» appeared in the centre section. I clicked OK
twice. I switched the macro recorder off.
I printed the page and the selected image appeared at the bottom.
The important code saved by the macro recorder was:
ActiveSheet.PageSetup.CenterFooterPicture.Filename = _
"C:UsersPublicPicturesSample PicturesDesert Landscape.jpg"
Replace "C:UsersPublicPicturesSample PicturesDesert Landscape.jpg"
with filename of your choice.
The macro recorder is usually the easiest way of discovering statements like this.
answered Apr 2, 2012 at 16:51
Tony DallimoreTony Dallimore
12.3k7 gold badges31 silver badges61 bronze badges
1
For anybody viewing this in the future, I’ll share my code to copy a range and save it as a file on your computer, which can then be added to the footer. You can eliminate whatever bits you don’t want =)
Dim objPic As Shape
Dim objChart As Chart
Dim strTimeStamp As String
Dim strFileDest As String
20 Sheets(2).Activate
30 Sheets(2).Columns("R:T").AutoFit
40 Sheets(2).Rows("17:21").AutoFit
50 ActiveWindow.DisplayGridlines = False
60 Call Sheets(2).Range("S17", "U21").CopyPicture(xlScreen, xlPicture)
70 ActiveWindow.DisplayGridlines = True
80 Sheets(2).Shapes.AddChart
90 Sheets(2).Activate
100 Sheets(2).Shapes.Item(1).Select
110 Set objChart = ActiveChart
120 ActiveChart.Parent.Name = "FooterChart"
' For some reason, Excel occasionally tries to make an actual chart out of these strings.
' It's just a nonsensical chart that messes the footer up but I'm having trouble duplicating the issue and figuring out what causes it.
' This should always work. Don't use .Clear, it crashes.
130 ActiveChart.ChartArea.ClearContents
140 objChart.Paste
150 Selection.Name = "FooterImage"
160 ActiveSheet.ChartObjects("FooterChart").Activate
170 Sheets(2).Shapes.Item(1).Line.Visible = msoFalse
180 Sheets(2).Shapes.Item(1).Height = Range("S17", "U21").Height
190 Sheets(2).Shapes.Item(1).Width = Range("S17", "U21").Width
200 ActiveChart.Shapes.Range(Array("FooterImage")).Height = Range("S17", "U21").Height
210 ActiveChart.Shapes.Range(Array("FooterImage")).Width = Range("S17", "U21").Width
220 Sheets(2).Shapes.Item(1).Height = Sheets(2).Shapes.Item(1).Height * 1.25
230 Sheets(2).Shapes.Item(1).Width = Sheets(2).Shapes.Item(1).Width * 1.25
240 ActiveChart.Shapes.Range(Array("FooterImage")).Height = ActiveChart.Shapes.Range(Array("FooterImage")).Height * 1.2
250 ActiveChart.Shapes.Range(Array("FooterImage")).Width = ActiveChart.Shapes.Range(Array("FooterImage")).Width * 1.2
260 strTimeStamp = CStr(Format(Now(), "yyyymmddHhNnSs"))
270 strFileDest = "D:Temp" & strTimeStamp & ".jpg"
280 objChart.Export strFileDest
290 InsertPicture strFileDest
300 If Len(Dir$(strFileDest)) > 0 Then
310 Kill strFileDest
320 End If
330 Sheets(2).Shapes.Item(1).Delete
answered Jul 13, 2015 at 13:04
Try this:
Dim ws as Worksheet
Set ws = Worksheets("YourWorksheetName")
With ws.PageSetup
.CenterFooterPicture = "&G" 'Specifies that you want an image in your footer
.CenterFooterPicture.Filename = "C:PicturesMyFooterImage.jpg" 'specifies the image file you want to use
End With
The code generated by the macro recorder will get you part of the way there, but as is often the case, it doesn’t provide the whole or most appropriate solution. It also sounds like you are trying to insert an image generated by Excel (such as a chart) into the footer? if that’s the case, I believe you will have to same the object as an image and then reference that image file.
answered Sep 30, 2014 at 21:42
If you want to insert certain information in the header / footer of the worksheet like the file name / file path or the current date or page number, you can do so using the below code. If it is just one worksheet you can do it manually, but if it is multiple sheets or all sheets in the workbook which need this information to populated, you can do this using a simple vba macro / code.
This sample macro will insert a header/footer in every worksheet in the active workbook. It will also insert the complete path to the workbook.
Option Explicit Sub InsertHeaderFooter() Dim wsAs Worksheet Application.ScreenUpdating = False Each wsInThisWorkbook.Worksheets With ws.PageSetup .LeftHeader = “Company Name:” .CenterHeader = “Page &P of &N” .RightHeader = “Printed &D &T” .LeftFooter = “Path : “ &ActiveWorkbook.Path .CenterFooter = “Workbook Name: & F” .RightFooter = “Sheet: &A” End With Next ws Set ws = Nothing Application.ScreenUpdating = True End Sub
To copy this code to your workbook, press Alt + F11 on your keyboard. Then on the left hand side, you will see Microsoft Excel Objects. Right click and select Insert. Then click on Module and copy this code to the code window on the right.
Lets break up each part of the code –
We start with the usual Dim statement where we declare the variables. In this case, we have only 1 variable – ws for the worksheet. Then we disable screen updating.
Now, in the FOR loop, we loop through each worksheet in the workbook which contains the macro. And we setup each parameter in Page Setup. &P, &N, &D, &T, &F and &A are certain format codes which can be applied to headers & footers. &P prints the page number. &N prints the total number of pages in the document. &D prints the current date. &T prints the current time. &F prints the name of the document and &A prints the name of the workbook tab.
At the end we set the worksheet to nothing and free the object and enable screen updating.
Here are 2 pictures. The 1st one shows you the header and the 2nd one the footer after the macro has been run.
The header has the label Company Name. The name is not entered in yet since we haven’t linked it to any cell or fed in any text for the Company Name. If you enter anything in the code or in the excel sheet and reference it, then the name will be picked up and populated here.
Page # of 0 shows that currently we have 0 pages in the file, since we have run this code on a blank file. If you run this code on a file containing data, it will show you the page number.
Printed <Date><Time> gives you the date and time the macro was run along with the text “Printed”.
In the Footer, the Path label will show you the path of the current file.
Our filename is Book1.xlsx which is currently an unsaved file. Hence there is no path showing up for the Path label.
The Sheet number is populated to the right of the footer.
If you liked our blogs, share it with your friends on Facebook. And also you can follow us on Twitter and Facebook.
We would love to hear from you, do let us know how we can improve, complement or innovate our work and make it better for you. Write us at info@exceltip.com
Below we will look at a program in Excel VBA that creates a footer before printing a workbook.
Create a Workbook BeforePrint Event. Code added to the Workbook BeforePrint Event will be executed by Excel VBA before you print a workbook.
1. Open the Visual Basic Editor.
2. Double click on This Workbook in the Project Explorer.
3. Choose Workbook from the left drop-down list. Choose BeforePrint from the right drop-down list.
4. To create a footer, add the following code line to the Workbook BeforePrint Event:
ActiveSheet.PageSetup.LeftFooter = ActiveWorkbook.FullName
Note: The PageSetup object contains all page setup attributes (left footer, bottom margin, paper size, left margin, and so on) as properties.
5. Test the program by printing your workbook.
Result:
This post will guide you how to insert the contents of a paricular into the header or footer cell in your active worksheet in Excel. How do I put a cell value into header or footer in all worksheets in your active workbook using VBA Macro in Excel.
You can easily add the number of pages, the current date and time or the name of current file, including the File Full path into the header or footer in your Excel. And there is no built-in command or function to add a cell value into the header or footer. How to do it. You can use an Excel VBA Macro
to insert cell Content into a paricular worksheet or all worksheet in your workbook quickly.
Table of Contents
- 1. Insert cell Content into Header or Footer in a Worksheet
- 2. Insert Cell Content into Header or Footer in All Worksheets
- 3. Video: Insert Cell Content into Header or Footer in Excel
To put a cell value into the header or footer in your current worksheet in Excel, and you can do the following steps:
Step1: open your excel workbook and then click on “Visual Basic
” command under DEVELOPER
Tab, or just press “ALT+F11
” shortcut.
Step2: then the “Visual Basic Editor
” window will appear.
Step3: click “Insert
” ->”Module
” to create a new module.
Step4: paste the below VBA code into the code window. Then clicking “Save
” button.
Sub InsertCellValueIntoHeader() Dim myRange As Range Set myRange = Application.Selection Set myRange = Application.InputBox("Select One Single Cell that you want to put its into Header or Footer", "InsertCellValueIntoHeader", myRange.Address, Type:=8) Application.ActiveSheet.PageSetup.LeftHeader = myRange.Value End Sub
Step5: back to the current worksheet, click on Macros
button under Code
group. then click Run button.
Step6: Select One Single Cell that you want to put its into Header or Footer. click on Ok button.
Step7: let’s see the last result:
If you want to add a cell value into the footer in your current worksheet, and you can use the following VBA Macro.
Sub InsertCellValueIntoFooter() Dim myRange As Range Set myRange = Application.Selection Set myRange = Application.InputBox("Select One Single Cell that you want to put its into Header or Footer", "InsertCellValueIntoHeader", myRange.Address, Type:=8) Application.ActiveSheet.PageSetup.LeftFooter = myRange.Value End Sub
If you need to add a cell value into the RightHeader
or RightFooter
, and you just need to change one code line(Application.ActiveSheet.PageSetup.LeftHeader
or Application.ActiveSheet.PageSetup.LeftFooter
) in above VBA Function as below line:
Application.ActiveSheet.PageSetup.RigthHeader = myRange.Value
or
Application.ActiveSheet.PageSetup.RightFooter = myRange.Value
2. Insert Cell Content into Header or Footer in All Worksheets
You can also insert a cell value into the header or footer for all worksheets in your active workbook using a VBA Macro in Excel. see belows:
Sub InsertCellValueIntoHeaderForAllSheets() Dim myRange As Range Set myRange = Application.Selection Set myRange = Application.InputBox("Select One Single Cell that you want to put its into Header or Footer", "InsertCellValueIntoHeader", myRange.Address, Type:=8) For Each mysheet In Application.ActiveWorkbook.Worksheets mysheet.PageSetup.LeftHeader = myRange.Value Next End Sub
If you need to insert cell content into footer for all worksheets, and you can use the following VBA Macro:
Sub InsertCellValueIntoFooterForAllSheets() Dim myRange As Range Set myRange = Application.Selection Set myRange = Application.InputBox("Select One Single Cell that you want to put its into Header or Footer", "InsertCellValueIntoHeader", myRange.Address, Type:=8) For Each mysheet In Application.ActiveWorkbook.Worksheets mysheet.PageSetup.LeftFooter = myRange.Value Next End Sub
3. Video: Insert Cell Content into Header or Footer in Excel
If you want to learn how to insert cell content into header or footer in Excel, this video will show you a simple and effective way using VBA code.
Skip to content
This tutorial shows how to insert the sheet name into a footer in a specific sheet using Excel and VBA
EXCEL METHOD 1. Insert sheet name into footer
EXCEL
VBA METHOD 1. Insert sheet name into footer using VBA
VBA
Sub Insert_sheet_name_footer()
‘declare a variable
Dim ws As Worksheet
Set ws = Worksheets(«Sheet1»)
With ws.PageSetup
.CenterFooter = «&A»
End With
End Sub
NOTES
Note 1: This VBA code will insert the sheet name into the center footer area of «Sheet1».
Related Topic | Description | Related Topic and Description |
---|---|---|
Insert a header | How to insert a header | |
Insert a footer | How to insert a footer in a specific sheet | |
Insert current date into footer | How to insert the current date into a footer in a specific sheet | |
Insert page numbers into footer | How to insert page numbers into a footer in a specific sheet | |
Insert sheet name into header | How to insert the sheet name into a header in a specific sheet |