Set up print area and avoid printing blank pages (VBA)

feni1388

Board Regular
Joined
Feb 19, 2018
Messages
133
Office Version
  1. 2021
Platform
  1. Windows
Hello...

I'm using the vba code below to set up print area and avoid printing blank pages, but it doesn't seem to work.
Can someone please help?

Column A to S is fixed but the row can sometimes short or long. When it's short, all the blank pages got printed too.

Dim rng As Range
Dim k As Range
On Error Resume Next
k = Cells(Rows.Count, 1).End(xlUp).Row
Set rng = ActiveSheet.Range("A1:S" & k)

With ActiveSheet.PageSetup
.PrintArea = rng.Address
.Orientation = xlLandscape
End With

Application.ScreenUpdating = True
 
@feni1388,
So what is the text, the cell address is written in the message in this line:
VBA Code:
MsgBox Cells(k, 1).Address <----- Insert this line
from the 3rd post from the user @jolivanes??? Can you answer?
 
Upvote 0

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Read the 3rd and 4th line in Post #3 again.
Sorry, I misunderstood it.
There's nothing shown in the message box or there's no message box actually.

When I run the macro line by line and hover my cursor...... on the Rows.Count part it shows 1048576.
k = Cells(Rows.Count, 1).End(xlUp).Row

then on this part, it shows k=nothing
MsgBox Cells(k, 1).Address
 
Upvote 0
Code:
Dim k As Range    '<---- This should be Long, not Range
On Error Resume Next    '<---- Delete this line
k = Cells(Rows.Count, 1).End(xlUp).Row
 
Upvote 0
Do you get white space with this?
Code:
Sub Check_It()
With ActiveSheet
    .PageSetup.PrintArea = "A1:S" & .Cells(.Rows.Count, 1).End(xlUp).Row
    .PrintOut , , , -1
End With
End Sub
 
Upvote 0
Now tell us the whole story.
You set the print area from A1 to Column S and the last used cell in Column A.
How many columns and rows without data are in this range?
 
Upvote 0
@feni1388,
Better yet, attach your original example file without confidential data (Replace with other data) to this topic! We are now playing the game “Guess what’s in my left hand”. We are waiting for an example file from you
 
Upvote 0
Below is my sample file.
(it seems that there's no way that I can attach the whole file, so I only attached the sheet that need to be printed)

Note: the last used cell is column G. I modified the code so that it will count from column G. but still the same result

data sample.xlsm
ABCDEFGHIJKLMNOPQRST
1Trust PO20240513_Trust_PO.xlsxNew:20240513_Trust_PO_102418
2Order NoDateItemPacksBoxPriceTotalTotal per itemZip codeAddessCompany's nameStorePhone no.memoInvTotalBoxExisting:20240513_Trust_PO.xlsx
3HEC8182020240513GL773012467,3807,380340-0194A townTrust0480-31-8888check the price Other7,3801
4Total17,380
5HEC8181920240513GH775011356,7506,750340-0194A townTrust0480-31-8888check the price Other6,7501
6Total16,750
714,130
8
9
10
11
12
13
14
print
Cell Formulas
RangeFormula
K1K1=IF(Sheet1!D1="","",Sheet1!D1)
 
Upvote 0
No formulas in Column G?

What do you see with this?
Code:
Sub Check_It_B()
With ActiveSheet
    With .PageSetup
        .PrintArea = "A1:S" & .Cells(.Rows.Count, 7).End(xlUp).Row
        .Orientation = xlLandscape
        .Zoom = False 
        .FitToPagesTall = 1 
        .FitToPagesWide = 1 
    End With
.PrintOut , , , -1
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,111
Members
453,021
Latest member
Justyna P

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