Print Preview Visible Columns

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have code below to see the print Preview. I need the code amended so as to display only the range of the unhidden columns i.e visible data

When running the macro, the preview shows the hidden columns , which I do not want to see

for eg if Unhidden Range is A:B and I:J, then I only want to print A2 to last row in Col B and I2 to last row in Col J

It would be appreciated if someone could kindly amend my code


Code:
 Sub Print_Preview()
Sheets("Summary").Select
Range("A1").Select
  LR = Sheets("Summary").Cells(Rows.Count, "A").End(xlUp).Row
Sheets("Summary").Select
 With ActiveSheet.PageSetup
.PrintGridlines = True
.PrintArea = "A2:AC" & LR + 5
.PrintTitleRows = "$1:$1"
.PrintTitleColumns = ""
.LeftHeader = "&D&T"
.CenterHeader = "SVC and Parts Turnover"
.Orientation = xlLandscape
.FitToPagesWide = 1
End With

Sheets("Summary").Select

ActiveWindow.View = xlPageBreakPreview
ActiveSheet.UsedRange.EntireColumn.AutoFit
      Range("A1").Select
      
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Hi howard,

how about

VBA Code:
Sub Print_Preview_mod()
' https://www.mrexcel.com/board/threads/print-preview-visible-columns.1225003/
Dim lr As Long
With Sheets("Summary")
  lr = .Cells(Rows.Count, "A").End(xlUp).Row
  With .PageSetup
    .PrintGridlines = True
    .PrintArea = "A2:AC" & lr + 5
    .PrintTitleRows = "$1:$1"
    .PrintTitleColumns = ""
    .LeftHeader = "&D&T"
    .CenterHeader = "SVC and Parts Turnover"
    .Orientation = xlLandscape
    .FitToPagesWide = 1
  End With
  .UsedRange.SpecialCells(xlCellTypeVisible).EntireColumn.AutoFit

  ActiveWindow.View = xlPageBreakPreview
End With
End Sub

Ciao,
Holger
 
Upvote 0
Many thanks Holger

I need you to make one change that I did not think abount before posting

I want to print preview onl;y visible columns that contains data in row1 for eg if the unhidden columns are A:B and Col F onwards and there is data in A1:B1 and F1:J1, only those columns must display in the page preview up to the last row conmtaining data in Col A


Kindly amend your code accordingly
 
Upvote 0
Hi howard,

like this

VBA Code:
Sub Print_Preview_mod02()
' https://www.mrexcel.com/board/threads/print-preview-visible-columns.1225003/
Dim lr As Long
Dim rngCell As Range

With Sheets("Summary")
  lr = .Cells(Rows.Count, "A").End(xlUp).Row
  With .PageSetup
    .PrintGridlines = True
    .PrintArea = "A2:AC" & lr + 5
    .PrintTitleRows = "$1:$1"
    .PrintTitleColumns = ""
    .LeftHeader = "&D&T"
    .CenterHeader = "SVC and Parts Turnover"
    .Orientation = xlLandscape
    .FitToPagesWide = 1
  End With
  For Each rngCell In .Range("A1:AC1")
    If Trim(rngCell) = vbNullString Then rngCell.EntireColumn.Hidden = True
  Next rngCell
  .UsedRange.SpecialCells(xlCellTypeVisible).EntireColumn.AutoFit
  ActiveWindow.View = xlPageBreakPreview
End With
End Sub

Ciao,
Holger
 
Upvote 0
Solution
Many thanks Holder

Your code works perfectly
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

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