PrintArea = "TableName" but visible cells only

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
929
Office Version
  1. 365
Platform
  1. Windows
I have this code that adjusts the print area based on the report the person is wanting to print, but lately it is printing double the pages. For example if the report happens to be 7 pages, it will have 7 pages plus an additional 7 that are blank when I look at print preview.

I am wondering if this is because of the columns that are hidden in the macro.

I am not sure 100% this is the problem but figured it was a good start

So my question is: Is there a way to set the PrintArea for a table for visible cells only?
See below code and thank you to anyone who can help!

VBA Code:
Sub Hearing_Testing()
'
' Hearing_Testing Macro
'

    Columns("A:A").EntireColumn.Hidden = True
    Columns("D:F").EntireColumn.Hidden = True
    Columns("H:H").EntireColumn.Hidden = True
    Columns("K:P").EntireColumn.Hidden = True
With ActiveSheet.PageSetup
        .PrintTitleRows = ""
        .PrintTitleColumns = ""
    End With
    ActiveSheet.PageSetup.PrintArea = "Emp_Listing[#All]" 'wondering if this line is my issue, printing the whole table even the hidden columns?? Change to visible only?
    Application.CutCopyMode = False
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .PrintTitleRows = "$2:$2"
        .PrintTitleColumns = ""
    End With
            With ActiveSheet.PageSetup
        .LeftHeader = ""
        .CenterHeader = ""
        .RightHeader = ""
        .LeftFooter = ""
        .CenterFooter = ""
        .RightFooter = ""
        .LeftMargin = Application.InchesToPoints(0.354330708661417)
        .RightMargin = Application.InchesToPoints(0.354330708661417)
        .TopMargin = Application.InchesToPoints(0.984251968503937)
        .BottomMargin = Application.InchesToPoints(0.984251968503937)
        .HeaderMargin = Application.InchesToPoints(0.511811023622047)
        .FooterMargin = Application.InchesToPoints(0.511811023622047)
        .PrintHeadings = False
        .PrintGridlines = False
        .PrintComments = xlPrintNoComments
        .CenterHorizontally = False
        .CenterVertically = False
        .Orientation = xlPortrait
        .Draft = False
        .PaperSize = xlPaperLetter
        .FirstPageNumber = xlAutomatic
        .Order = xlDownThenOver
        .BlackAndWhite = False
        .Zoom = 120
        .PrintErrors = xlPrintErrorsDisplayed
        .OddAndEvenPagesHeaderFooter = False
        .DifferentFirstPageHeaderFooter = False
        .ScaleWithDocHeaderFooter = True
        .AlignMarginsHeaderFooter = True
        .EvenPage.LeftHeader.Text = ""
        .EvenPage.CenterHeader.Text = ""
        .EvenPage.RightHeader.Text = ""
        .EvenPage.LeftFooter.Text = ""
        .EvenPage.CenterFooter.Text = ""
        .EvenPage.RightFooter.Text = ""
        .FirstPage.LeftHeader.Text = ""
        .FirstPage.CenterHeader.Text = ""
        .FirstPage.RightHeader.Text = ""
        .FirstPage.LeftFooter.Text = ""
        .FirstPage.CenterFooter.Text = ""
        .FirstPage.RightFooter.Text = ""
    End With
    Application.PrintCommunication = True
    
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Check the following:

1689607209580.png


-----------------​

In the code you have the zoom property at 120, perhaps that is causing the sheet on the right side to overflow and that implies the printing of more sheets.
Try 100

VBA Code:
        .Zoom = 120

---------------​

Also try the following, replace this line:
VBA Code:
        .Zoom = 120

For these:

VBA Code:
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 7                 '
I put the number 7 only to do the test, we must rule out the possible problems.

---------------------------​
Other option:

Replace this line:
VBA Code:
ActiveSheet.PageSetup.PrintArea = "Emp_Listing[#All]"

By these lines:
VBA Code:
  Dim lr As Long, lc As Long
  lr = ActiveSheet.ListObjects("Emp_Listing").Range.Rows.Count
  lc = ActiveSheet.ListObjects("Emp_Listing").Range.Columns.Count
  lr = Range("A1", Cells(Rows.Count, lc)).Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
  ActiveSheet.PageSetup.PrintArea = Range("A1", Cells(lr, lc)).Address






--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------

 
Upvote 0
Solution
Check the following:

View attachment 95421

-----------------​

In the code you have the zoom property at 120, perhaps that is causing the sheet on the right side to overflow and that implies the printing of more sheets.
Try 100

VBA Code:
        .Zoom = 120

---------------​

Also try the following, replace this line:
VBA Code:
        .Zoom = 120

For these:

VBA Code:
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 7                 '
I put the number 7 only to do the test, we must rule out the possible problems.

---------------------------​
Other option:

Replace this line:
VBA Code:
ActiveSheet.PageSetup.PrintArea = "Emp_Listing[#All]"

By these lines:
VBA Code:
  Dim lr As Long, lc As Long
  lr = ActiveSheet.ListObjects("Emp_Listing").Range.Rows.Count
  lc = ActiveSheet.ListObjects("Emp_Listing").Range.Columns.Count
  lr = Range("A1", Cells(Rows.Count, lc)).Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
  ActiveSheet.PageSetup.PrintArea = Range("A1", Cells(lr, lc)).Address






--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------


This seems to have fixed the problem:

VBA Code:
        .Zoom = False
        .FitToPagesWide = 1
        .FitToPagesTall = 7

Thank you very much :giggle:
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
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