Set print area w/variables

bobkap

Active Member
Joined
Nov 22, 2009
Messages
313
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
I am trying to include a print area in a macro, BUT I want to use variables vs a fixed range. It cannot be that difficult, but I just cannot figure it out. If I use Range function with Cells it bombs out. I tried recording what I want to do, thinking I could edit it to do what I want, but that didn't work either.

Can someone please show me how to use variables when creating a print area?

Thanks.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
I usually find it works best to create a range variable, then set the range you want with that variable.
Then use that variable in your print command.

If you run into problems, please post your code and the specifics as how to exactly dynamically determine the range you want to print.
 
Upvote 0
For ex. this set your print area as range of A1: LastColumn*LastRow on activesheet

VBA Code:
Sub CreatePrintArea()
Dim PrnArea As Range
Dim lastCol As Integer
Dim lastR As Integer
With ActiveSheet
    lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set PrnArea = .Range(Cells(1, 1), Cells(lastRow, lastCol))
    .PageSetup.PrintArea = PrnArea.Address
End With
End Sub
 
Upvote 0
For ex. this set your print area as range of A1: LastColumn*LastRow on activesheet

VBA Code:
Sub CreatePrintArea()
Dim PrnArea As Range
Dim lastCol As Integer
Dim lastR As Integer
With ActiveSheet
    lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
    lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    Set PrnArea = .Range(Cells(1, 1), Cells(lastRow, lastCol))
    .PageSetup.PrintArea = PrnArea.Address
End With
End Sub
Many thanks!
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,715
Members
448,985
Latest member
chocbudda

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