Dynamic Print Range With Print Preview

Pete0922

New Member
Joined
Jun 29, 2018
Messages
5
New To VBA

I wrote a macro to print a specific range on a specific sheet:

Sub PrintPreview()
If ActiveSheet.Name <> "Sheet2" Then _
Sheets("database").Range("C1:AE12").PrintPreview
End Sub

However, the table I am attempting to print will have additional rows added (number of columns will remain constant). So I then created a dynamic range called "Print_Area". However, I can't figure out how to incorporate the dynamic range into my macro.

Thanks in advance
 

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
Code:
[COLOR=#333333]Sheets("database").Range(Print_Area).PrintPreview[/COLOR]
if you created as a vba variable

Code:
[COLOR=#333333]Sheets("database").Range("Print_Area").PrintPreview[/COLOR]
if you created using the name manager
 
Upvote 0
Alan,

Thanks for the quick reply. Unfortunately, I get an error when I run it.

First of all; I gave you some bad info, the dynamic range is called "PivotTable" not "Print_Area".

This is what the macro looks like now:

Sub PrintPreview()
If ActiveSheet.Name <> "Sheet2" Then _
Sheets("database").Range("PivotTable").PrintPreview
End Sub

When I run the macro, I get the following error message: "Run time error; 1004, Application defined or object defined error.

Thanks.
 
Upvote 0
So, "PivotTable" is a named range and not a variable in VBA?
 
Upvote 0
The named range was created with a formula.
Please explain this as I don't understand how this happens. Ranges are usually named using the NameManager.
 
Upvote 0
I could not replicate your issue. I built a sample workbook. Named a Range "PivotTable". Used your code and it showed the range as the print preview. So your code is fine, you have some other issue. Try creating a new workbook and use the Name Manager.

On the Ribbon, Select Formulas, Select Define Name and highlight your range. This is what I did and then ran your code.
 
Upvote 0

Forum statistics

Threads
1,216,109
Messages
6,128,880
Members
449,477
Latest member
panjongshing

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