Page Number in cell

John117

Active Member
Joined
Sep 29, 2004
Messages
371
Hello
I have a template spanning between Col “A” and Col “DH”. This template is populated with data from TXT files. Sometimes I need to print only 1 page and other times lets say 8 pages, which depends on amount of data I am importing into this template.
Here is my code to print the correct # of pages:
Code:
Private Sub Worksheet_Calculate()

Select Case Range("num_pages").Value
Case 1
ActiveSheet.PageSetup.PrintArea = "$A$1:$M$45"
Case 2
ActiveSheet.PageSetup.PrintArea = "$A$1:$X$45"
Case 3
ActiveSheet.PageSetup.PrintArea = "$A$1:$AI$45"
Case 4
ActiveSheet.PageSetup.PrintArea = "$A$1:$AT$45"
Case 5
ActiveSheet.PageSetup.PrintArea = "$A$1:$BE$45"
Case 6
ActiveSheet.PageSetup.PrintArea = "$A$1:$BP$45"
Case 7
ActiveSheet.PageSetup.PrintArea = "$A$1:$CA$45"
Case 8
ActiveSheet.PageSetup.PrintArea = "$A$1:$CL$45"
Case 9
ActiveSheet.PageSetup.PrintArea = "$A$1:$CW$45"
Case 10
ActiveSheet.PageSetup.PrintArea = "$A$1:$DH$45"
End Select
End Sub
Does anybody knows how to display in cell (not using header / footer function) page number, i.e. “Page 1 of 2” if I am printing only 2 pages or “Page 1of 8” if I am printing lets say 8 pages.
Thanks
John
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
YOu can add the desired result to a cell using your case select construct based on the following example:

Case 1
ActiveSheet.PageSetup.PrintArea = "$A$1:$M$45"
ActiveSheet.Range("m45") = "Page 1 of 1"
Case 2
ActiveSheet.PageSetup.PrintArea = "$A$1:$X$45"
ActiveSheet.Range("M45") = "Page 1 of 2"
ActiveSheet.Range("X45") = "Page 2 of 2"


Assuming you wanted the page 1 of ? to be in the bottom right of the printed area.

Why not just use page footer and use the #page of #pages method? You would not have to add any code or update it every time, as it only applies to printed sheets which is covered by your Print Area code .
 
Upvote 0
Hi John :

If your interested , here's a method that would allow you to get rid of your case statement.

Code:
Private Sub Worksheet_Calculate()
    Dim PrintRng As String

    PrintRng = Range("A1").Address & ":" & _
    Cells(45, 13 + (11 * (Range("num_pages").Value - 1))).Address
    
    ActiveSheet.PageSetup.PrintArea = PrintRng


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,247
Messages
6,177,406
Members
452,774
Latest member
Macca1962

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