How to Print Titles in Excel Except for Last Page (2 Easy Macros)

Get FREE Advanced Excel Exercises with Solutions!

Printing titles on every page in Excel is much easier as there is a built-in command in Excel to do that. But if we want to print titles on every page except the last page then there is no built-in command in Excel. So we must use Visual Basic for Applications (VBA) to perform this kind of specific task. In this article, I’ll show 2 simple VBA Macros to print titles in Excel except the last page following some simple steps.


Print Titles Except for Last Page in Excel: 2 VBA Macros

First of all, get introduced to our dataset which represents some salesperson’s sales in different regions.


Macro 1: Specify Title Row in Codes to Print Titles Except for Last Page

In our first macro, we’ll specify the title rows in the codes so that we don’t need to select them after running the codes. When you work with an Excel file that has the same title row on every sheet then this macro will be helpful for you although you can change it in the codes.

Steps:

  • I want to print the dataset in three sheets, so first, we’ll have to insert page breaks. Click on the row from where you want to create a break. I selected row 10.
  • Then click as follows: Page Layout > Breaks > Insert Page Break.

Specify Title Row in Codes to Print Titles Except for Last Page

Soon after, you will see a line along the upper border of that row.

Specify Title Row in Codes to Print Titles Except for Last Page

  • Follow the same procedures to insert another page break. I inserted another one before row 15.

Now our dataset is ready to print in three sheets.

Specify Title Row in Codes to Print Titles Except for Last Page

  • After that, press ALT +F11 to open the VBA window.
  • Next, click as follows to insert a new module: Insert > Module.

Specify Title Row in Codes to Print Titles Except for Last Page

  • Later, type the following codes in the module-
Sub Specify_Title_Row_in_Codes()
Dim AllPages As Long
AllPages = Application.ExecuteExcel4Macro("GET.DOCUMENT(50)")
With ActiveSheet.PageSetup
.PrintTitleRows = "$4:$4"
ActiveSheet.PrintOut from:=1, To:=AllPages - 1
.PrintTitleRows = ""
ActiveSheet.PrintOut from:=AllPages, To:=AllPages
End With
End Sub
  • Then go back to your sheet.

Specify Title Row in Codes to Print Titles Except for Last Page

Code Breakdown:

  • Here firstly, I started a Sub procedure- Specify_Title_Row_in_Codes.
  • Then declare a variable AllPages As Long.
  • Then used ExecuteExcel4Macro(“GET.DOCUMENT(50)”) to find the total number of printed pages.
  • Next, use PageSetup.PrintTitleRows to determine the row number.
  • Finally, PrintOut will print every page with titles except the last page.

  • From the Developer ribbon, click Macros to open the Macro dialog box.

Specify Title Row in Codes to Print Titles Except for Last Page

  • Then just select the macro name and press Run.

Soon after, you will get a dialog box to save the printed PDF file. First, it will save without the last page. Then again the dialog box will appear to save the last page separately.

Specify Title Row in Codes to Print Titles Except for Last Page

  • Choose your destination folder and type the file name for the pdf file without the last page.
  • Finally, just press Save.

Specify Title Row in Codes to Print Titles Except for Last Page

  • Then set another name to save the last page and press Save.

Specify Title Row in Codes to Print Titles Except for Last Page

Here’s the printed first page of the dataset.

It’s the second page, and see, the titles have been added after the page break.

It’s the separate printed pdf of the last page without the titles.

Read More: How to Set Multiple Rows as Print Titles in Excel 


Macro 2: Print Titles with Row Selection Except for Last Page

If you have an Excel file that has many titles in different rows then this macro will be useful because after running the codes you will be able to set the title rows by selection before printing.

Steps:

Sub Print_Titles_with_Row_Selection()
Dim mPages As Long, I As Integer, mRange As Range
On Error Resume Next
mPages = ActiveSheet.PageSetup.Pages.Count
Set mRange = Application.InputBox("Select Title Rows to Repeat:", "Print Titles Except Last Page", , , , , , Type:=8)
If mRange Is Nothing Then Exit Sub
On Error Resume Next
If mPages > 0 Then
With ActiveSheet.PageSetup
.PrintTitleRows = mRange.AddressLocal
ActiveSheet.PrintOut from:=1, To:=mPages - 1
.PrintTitleRows = ""
ActiveSheet.PrintOut from:=mPages, To:=mPages
End With
End If
End Sub
  • Next, go back to your sheet.

Print Titles with Row Selection Except for Last Page in Excel

Code Breakdown:

  • First, I created a Sub procedure- Print_Titles_with_Row_Selection.
  • After that, declared some variables, mPages As Long, I As Integer, and mRange As Range.
  • Then used PageSetup.Pages.Count to count all the pages from the active sheet.
  • Later, used InputBox to create an input box to select the title row.
  • Finally, PrintOut will print every page with titles except the last page.

Print Titles with Row Selection Except for Last Page in Excel

  • Soon after, a dialog box will appear to select the title row. Click on the row number and press OK.

Print Titles with Row Selection Except for Last Page in Excel

Then you will see the output like the image below. It’s our first page.

The second page includes the titles.

Here’s the separate file with the last page without the titles.

Read More: How to Select Column A as Titles to Repeat on Each Page


Download Practice Workbook

You can download the free Excel workbook from here and practice on your own.


Conclusion

That’s all for the article. I hope the procedures described above will be good enough to print titles in Excel except for the last page. Feel free to ask any question in the comment section and please give me feedback.


Related Articles

Md. Sourov Hossain Mithun
Md. Sourov Hossain Mithun

Hello! I am Md. Sourov Hossain Mithun. Currently, I am working at Exceldemy as an Excel and VBA Content Developer. Excel is an amazing software to learn or work. Here, I will post Excel related useful articles. I am a graduate of Bangladesh University of Engineering and Technology. I love to learn new things and work with them. Thank you.

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo