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?
 
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)

I diagnosed it wrong. It's not the spaces, it's the multiple sheets. Spaces are ok. Even multiple references to the same sheet is ok.
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I just dont think its possible to name a range like that... If it is, I do not know how.

I don't get it. All I want to do is put "Range1" in cell L1 and then to print whatever cells are listed under the "Refers To" column for that particular "Name." It works, just not when a range contains cells from multiple excel sheets.
 
Upvote 0
ABCD
1Range1Sheet1!A1:A5
2Range2Sheet1!B60:X70Sheet2!A50:A52'Sheet 3'!D10:E15
3Range3'Sheet 3'!C1:C20

<tbody>
</tbody>
Why not Make a Helper table in a different sheet

Then use that to discover your ranges

Match to find the range you typed into L2 Then loop through the following columns in that row and print the range if its not blank.
 
Upvote 0
ABCD
1Range1Sheet1!A1:A5
2Range2Sheet1!B60:X70Sheet2!A50:A52'Sheet 3'!D10:E15
3Range3'Sheet 3'!C1:C20

<tbody>
</tbody>
Why not Make a Helper table in a different sheet

Then use that to discover your ranges

Match to find the range you typed into L2 Then loop through the following columns in that row and print the range if its not blank.

That's kind of what I'm doing given that the way I wanted to do it appears to be impossible.
 
Upvote 0
Code:
Sub PRANGE()
Dim LookUpRow As Integer, i As Integer


LookUpRow = Application.Match(Sheets("something").Range("L2").Value, Sheets("Lookup").Range("A:A"), 0) 
'change Sheets("something") to the sheet where you input in L2...
 
For i = 2 To 10 'change the last number based on the maximum number of sheets any named range could contain
    If Sheets("Lookup").Cells(LookUpRow, i).Value <> "" Then
        Sheets(Replace(Split(Sheets("Lookup").Cells(LookUpRow, i).Value, "!")(0), "'", "")).Select
        ActiveSheet.Range(Split(Sheets("Lookup").Cells(LookUpRow, i).Value, "!")(1)).PrintOut Preview:=True
    End If
Next i


End Sub
 
Upvote 0
Code:
Sub PRANGE()
Dim LookUpRow As Integer, i As Integer


LookUpRow = Application.Match(Sheets("something").Range("L2").Value, Sheets("Lookup").Range("A:A"), 0) 
'change Sheets("something") to the sheet where you input in L2...
 
For i = 2 To 10 'change the last number based on the maximum number of sheets any named range could contain
    If Sheets("Lookup").Cells(LookUpRow, i).Value <> "" Then
        Sheets(Replace(Split(Sheets("Lookup").Cells(LookUpRow, i).Value, "!")(0), "'", "")).Select
        ActiveSheet.Range(Split(Sheets("Lookup").Cells(LookUpRow, i).Value, "!")(1)).PrintOut Preview:=True
    End If
Next i


End Sub


I have an excel cell with the following value:

sheet1!$A:$J,'sheet 2'!$A:$G

How do I print that? What is the VB command? I am getting some errors.
 
Upvote 0
If all your ranges are in one cell seperated by commas like the example you gave me you can do this

Code:
Sub PRANGE()
Dim LookUpRow As Integer, i As Integer
Dim rPart As Variant




LookUpRow = Application.Match(Sheets("something").Range("L2").Value, Sheets("Lookup").Range("A:A"), 0)
'change Sheets("something") to the sheet where you input in L2...

rPart = Split(Sheets("Lookup").Cells(LookUpRow, 2).Value, ",")




 
For i = 0 To UBound(rPart)
        Sheets(Replace(Split(rPart(i), "!")(0), "'", "")).Select
        ActiveSheet.Range(Split(rPart(i), "!")(1)).PrintOut Preview:=True
Next i




End Sub
 
Upvote 0
If all your ranges are in one cell seperated by commas like the example you gave me you can do this

Code:
Sub PRANGE()
Dim LookUpRow As Integer, i As Integer
Dim rPart As Variant




LookUpRow = Application.Match(Sheets("something").Range("L2").Value, Sheets("Lookup").Range("A:A"), 0)
'change Sheets("something") to the sheet where you input in L2...

rPart = Split(Sheets("Lookup").Cells(LookUpRow, 2).Value, ",")




 
For i = 0 To UBound(rPart)
        Sheets(Replace(Split(rPart(i), "!")(0), "'", "")).Select
        ActiveSheet.Range(Split(rPart(i), "!")(1)).PrintOut Preview:=True
Next i




End Sub

Subscript out of range error.
 
Upvote 0
What is written in your lookup table next to the range name you are looking up. Can you copy the full address you typed in



It might be because you put a space after the comma separating the sheets.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,326
Messages
6,124,270
Members
449,149
Latest member
mwdbActuary

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