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

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
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
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,223,893
Messages
6,175,244
Members
452,622
Latest member
Laura_PinksBTHFT

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