VBA for printing select range with page formatting

FutureJack031

New Member
Joined
Dec 21, 2018
Messages
1
Good morning,

I'm looking for a VBA code that will print a select range of cells on the active sheet and will format the printed sheet to fit everything on 1 page in portrait orientation.

Any direction will be much appreciated.

Thanks
DV
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Code:
Sub btnPrint_Click()


    Dim rng As Range


    Set rng = Selection
    rng.Select
    
    With rng.Worksheet
      'Do a crude assignment of paper orientation
      If rng.Height > rng.Width Then
      .PageSetup.Orientation = xlPortrait
      Else
        .PageSetup.Orientation = xlLandscape
      End If
      .PageSetup.PrintArea = rng.Address
      
      .PrintOut Preview:=True, IgnorePrintAreas:=False
      
    End With


End Sub
 
Upvote 0
Try...

Code:
Option Explicit

Sub PrintSelectedRange()


    If TypeName(Selection) <> "Range" Then
        MsgBox "Please select a range of cells, and try again!", vbExclamation
        Exit Sub
    End If
    
    Application.PrintCommunication = False
    With ActiveSheet.PageSetup
        .Orientation = xlPortrait
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
    Application.PrintCommunication = True
    
    Selection.PrintOut Copies:=1
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,334
Members
449,077
Latest member
Jocksteriom

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