VBA Print Preview only displaying 1 page

Jasesair

Active Member
Joined
Apr 8, 2015
Messages
282
Office Version
  1. 2016
Having a little trouble with the below code. It's only displaying one page when more data exists for a second print preview page.

VBA Code:
Sub PrintJuniors2021()
Dim LastRow As Long
Application.ScreenUpdating = False
Columns("AN:BQ").EntireColumn.Hidden = False
LastRow = Sheets("2021").Range("AO" & Rows.Count).End(xlUp).Row
Sheets("2021").PageSetup.PrintArea = "AO2:AT107" & LastRow
With ActiveSheet.PageSetup
    .Orientation = xlLandscape
    End With
ActiveSheet.PrintPreview
Columns("AN:BQ").EntireColumn.Hidden = True
End Sub
 
It's not showing anything. I have to click on 'show print preview' but then it's just blank pages. No change removing the .Zoom line.
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Any difference?

VBA Code:
Sub PrintJuniors2021()
    Dim LastRow As Long
    Application.ScreenUpdating = False
    Sheets("2021").Columns("AN:BQ").Hidden = False
    LastRow = Sheets("2021").Range("AO" & Rows.Count).End(xlUp).Row
   
    Sheets("2021").PageSetup.PrintArea = ""
    Application.PrintCommunication = False
   
    With Sheets("2021").PageSetup
        .Orientation = xlLandscape
        .PrintArea = Sheets("2021").Range("AO2:AT" & LastRow).Address
    End With
    Application.PrintCommunication = True
    Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
 
    'Sheets("2021").Columns("AN:BQ").Hidden = True
End Sub
 
Upvote 0
That works! It's still forcing me to click on 'Show Print Preview', and it isn't doing the last row at all - just printing all rows.

When I try to allow the hiding of the columns again, I'm back to getting blank sheets.

This is where I'm at now. Couple of adjustments to columns etc.

VBA Code:
Sub PrintJuniors2021()
    Dim LastRow As Long
    Application.ScreenUpdating = False
    Sheets("2021").Unprotect Password:=""
    Sheets("2021").Columns("Aq:av").Hidden = False
    LastRow = Sheets("2021").Range("Ar" & Rows.Count).End(xlUp).Row
   
    Sheets("2021").PageSetup.PrintArea = ""
    Application.PrintCommunication = False
   
    With Sheets("2021").PageSetup
        .Orientation = xlLandscape
        .PrintArea = Sheets("2021").Range("Aq1:Av" & LastRow).Address
    End With
    Application.PrintCommunication = True
    Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
'    Sheets("2021").Columns("Aq:av").Hidden = True
    Sheets("2021").Protect Password:=""
End Sub
 
Last edited:
Upvote 0
and it isn't doing the last row at all - just printing all rows
It is finding the Last row fine for me, what is in showing as the range for Print_Area under name manager once the macro has run?
It's still forcing me to click on 'Show Print Preview',
It doesn't for me in 365, it brings up
1614107656381.png
 
Upvote 0
What I do notice is the 2021 sheet has to be active when the code runs.


VBA Code:
Sub PrintJuniors2021()
    Dim LastRow As Long
    Application.ScreenUpdating = False
    Sheets("2021").Columns("AN:BQ").Hidden = False
    LastRow = Sheets("2021").Range("AO" & Rows.Count).End(xlUp).Row
   
    Sheets("2021").PageSetup.PrintArea = ""
    Application.PrintCommunication = False
   
    With Sheets("2021").PageSetup
        .Orientation = xlLandscape
        .PrintArea = Sheets("2021").Range("AO2:AT" & LastRow).Address
    End With
    Application.PrintCommunication = True
    Sheets("2021").Activate
    Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")

    'Sheets("2021").Columns("AN:BQ").Hidden = True
End Sub

Are you sure that you have nothing below your data in column AO? including formulas returning "".
 
Upvote 0
Ah yes, that's the issue! I've got an iferror returning ""... This is for all cells in the range.
 
Upvote 0
Rich (BB code):
Sub PrintJuniors2021()
    Dim LastRow As Long
    Application.ScreenUpdating = False
    Sheets("2021").Columns("AN:BQ").Hidden = False
    LastRow = Sheets("2021").Columns("AO").Find("*", , xlValues, , xlByRows, xlPrevious).Row
    Sheets("2021").PageSetup.PrintArea = ""
    Application.PrintCommunication = False
    
    With Sheets("2021").PageSetup
        .Orientation = xlLandscape
        .PrintArea = Sheets("2021").Range("AO2:AT" & LastRow).Address
    End With
    Application.PrintCommunication = True
    Sheets("2021").Activate
    Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
 
    'Sheets("2021").Columns("AN:BQ").Hidden = True
End Sub
 
Upvote 0
Solution
That's it! Is there any way for force the Show Print Preview? And, I'd really like to be able to automatically hide the columns after the macro has run?

This is the latest:

VBA Code:
Sub PrintJuniors2021()
    Dim LastRow As Long
    Application.ScreenUpdating = False
    Sheets("2021").Unprotect Password:=""
    Sheets("2021").Columns("Aq:av").Hidden = False
    LastRow = Sheets("2021").Columns("AR").Find("*", , xlValues, , xlByRows, xlPrevious).Row
   
    Sheets("2021").PageSetup.PrintArea = ""
    Application.PrintCommunication = False
   
    With Sheets("2021").PageSetup
        .Orientation = xlLandscape
        .PrintArea = Sheets("2021").Range("Aq1:Av" & LastRow).Address
    End With
    Application.PrintCommunication = True
    Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
'    Sheets("2021").Columns("Aq:av").Hidden = True
    Sheets("2021").Protect Password:=""
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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