VBA printing using a named print range

GerrypFL

New Member
Joined
Jul 1, 2020
Messages
6
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
I have created a named print range (dynamic range) that I want to create a VBA trigger button to print the named print range. I have attempted to create the VBA coding but end up with errors.

Sub PrintCells()
With Sheet6
.PageSetup.PrintArea = Range("PAYROLL_DEDUCTION_NOTIFICATION")
.PrintOut

End With
End Sub

I am struggling with how to use the named print range to preserve the dynamic nature. Excel has a bug in that if you reference the dynamic print range and someone makes any changes to the printer options the print range is converted to the last fixed range that was used. If I can call the named range when using the VBA code to print the dynamic range will always be valid. Any solution for using VBA to print the named range is appreciated.
 

Attachments

  • 2020-06-30_13-28-57.jpg
    2020-06-30_13-28-57.jpg
    91.3 KB · Views: 26

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Hi and welcome to MrExcel.
Try this

VBA Code:
Sub PrintCells()
  With Sheet6
    .PageSetup.PrintArea = Range("PAYROLL_DEDUCTION_NOTIFICATION").Address
    .PrintOut
  End With
End Sub
 
Upvote 0
Hi and welcome to MrExcel.
Try this

VBA Code:
Sub PrintCells()
  With Sheet6
    .PageSetup.PrintArea = Range("PAYROLL_DEDUCTION_NOTIFICATION").Address
    .PrintOut
  End With
End Sub


I gave it a try and ended up getting a Global fault error

2020-07-01_10-02-42.jpg
2020-07-01_10-05-09.jpg
 
Upvote 0
1593615010214.png


According to your formula to determine the named range:
The value of the rows is determined by the data you have in cell P4 of the "Criteria" sheet, then:

VBA Code:
Sub PrintCells()
  Dim vRow
  vRow = Sheets("criteria").Range("P4").Value
  With Sheet6
    .PageSetup.PrintArea = .Range("A1", .Cells(vRow, 5)).Address
    .PrintOut
  End With
End Sub
 
Upvote 0
View attachment 17380

According to your formula to determine the named range:
The value of the rows is determined by the data you have in cell P4 of the "Criteria" sheet, then:

VBA Code:
Sub PrintCells()
  Dim vRow
  vRow = Sheets("criteria").Range("P4").Value
  With Sheet6
    .PageSetup.PrintArea = .Range("A1", .Cells(vRow, 5)).Address
    .PrintOut
  End With
End Sub
Thanks!! That solved the issue.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
Sorry, is there coding that can be inserted into what you had suggested to keep scaling to print all of the columns in the range on one page?
 
Upvote 0
This does not work!

Sub NewPrinting()
Dim vRow
vRow = Sheets("criteria").Range("P4").Value

confirm = InputBox("Are you sure you want to print? (enter 'y' to continue)")

If confirm <> "y" Then GoTo TheEnd

With Sheet6
.PageSetup.PrintArea = .Range("A1", .Cells(vRow, 5)).Address
.FitToPagesWide = 1
.PrintOut
End With
TheEnd:

End Sub
 
Upvote 0
Try this

VBA Code:
Sub PrintCells()
  Dim vRow
  vRow = Sheets("criteria").Range("P4").Value
  If MsgBox("Are you sure you want to print?", vbYesNo + vbQuestion, "New Printing") = vbNo Then Exit Sub
  
  With Sheet6
    .PageSetup.PrintArea = .Range("A1", .Cells(vRow, 5)).Address
    With .PageSetup
      .Orientation = xlPortrait
      .FitToPagesWide = 1
      .FitToPagesTall = 1
    End With
    .PrintOut
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,684
Members
449,048
Latest member
81jamesacct

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