Print area based on input

primuspaul

Board Regular
Joined
Dec 23, 2015
Messages
75
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
I want to print certain areas of various sheets in a workbook based on user input.

Code:
      Sub PAREA()
         Dim xy As String
         xy = .Cells(L, 2).Text
         Range(xy).PrintOut Preview:=True
      End Sub

The file has multiple Names and those names refer to various parts of the file. Some Names refer to multiple areas on multiple sheets and some of those sheets have names with spaces in them (which also caused problems in my tests).

What is the right way to do this? L2 on some Sheet1 would have text like PrintArea1 or PrintArea2 and PrintArea1 and PrintArea2 would also be the names of some of those print area names. How do I do this?
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
is XY a userinput address? for example... do they type something like "B50:X60" into the cell?

Where do you get L... is that a global variable?

or are you trying to call the data input into Column L row 2? in which case it would be

xy = .Range("L2").Value

or

xy = .Cells(2, "L").Value
 
Last edited:
Upvote 0
is XY a userinput address? for example... do they type something like "B50:X60" into the cell?

Where do you get L... is that a global variable?

or are you trying to call the data input into Column L row 2? in which case it would be

xy = .Range("L2").Value

or

xy = .Cells(2, "L").Value

No, that cell will contain the name of a print area, such as "PrintArea1." In the Name Manager, if you look at "PrintArea1," it will link to cells A:A on "sheet 5" and cells B5:H17 on "sheet 8."
 
Upvote 0
I have success with

Code:
      Sub PAREA()
         Dim xy As String
         xy = ActiveSheet.Cells(2, "L").Value
         Range(xy).PrintOut Preview:=True
      End Sub

When the named PrintArea includes multiple locations they print to separate sheets.

But for spaces... you cannot name a range with a space in it. Do you mean that the User might insert "PrintArea A" but you want the code to recognise that as "PrintAreaA"?

Use
xy = Replace(ActiveSheet.Cells(2,"L").Value, " ", "")
 
Upvote 0
Code:
Sub PAREA()
         Dim xy As String
         xy = Range("L1").Value
         Range(xy).PrintOut Preview:=True
      End Sub
That works, but only with a Name that links to a print area that has sheets without spaces. Sheets named with something that has spaces fails with this error:

Run-time error '1004':

Method 'Range' of object '_Global' failed
 
Upvote 0
I have success with

Code:
      Sub PAREA()
         Dim xy As String
         xy = ActiveSheet.Cells(2, "L").Value
         Range(xy).PrintOut Preview:=True
      End Sub

When the named PrintArea includes multiple locations they print to separate sheets.
That's what I want.
But for spaces... you cannot name a range with a space in it. Do you mean that the User might insert "PrintArea A" but you want the code to recognise that as "PrintAreaA"?

Use
xy = Replace(ActiveSheet.Cells(2,"L").Value, " ", "")

No, I mean some of my sheets have spaces in them. I might have "Sheet1" "Sheet2" and then a "Sheet 3." So "PrintAreaA" would contain parts of "Sheet 3." Then I'd get the error.
 
Upvote 0
I'm getting errors any time my named range contains multiple sheets. I do not know why.
 
Last edited:
Upvote 0
I'm getting errors any time my named range contains multiple sheets. I do not know why.
No. It prints correctly when I select a named range that doesn't include sheet names with spaces, but as soon as I try to print a print area that includes sheet names with spaces, I get the error.
 
Upvote 0
Can you give me an example of the address of one of your named ranges without spaces that prints correctly and an example of the address of a named range that does not print (gives you an error)
 
Upvote 0
I'm getting errors any time my named range contains multiple sheets. I do not know why.

So maybe that's what's happening to me. Multiple sheets are causing it and not necessary spaces in sheet names.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,492
Members
448,967
Latest member
visheshkotha

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