VBA to select multiple dynamic ranges and set its print area

crakkus

New Member
Joined
May 28, 2019
Messages
14
Hello,

I have about 10 pivot tables in one sheet moving across the sheet from A1 to BM1. I am trying to create a macro that will select the top cell of each pivot table, ctrl+shift+down to select its dynamic depth, set the selection to an object, and then set the print area using a union. In theory it all makes sense but when I get to the union I get an error 438. Any reason why my code doesn't seem to work? Thank you in advance!

VBA Code:
Sub PrintArea()
'
' PrintArea Macro
'
Dim myCells1 As Range
Dim myCells2 As Range
Dim myCells3 As Range
Dim myCells4 As Range
Dim myCells5 As Range
Dim myCells6 As Range
Dim myCells7 As Range
Dim myCells8 As Range
Dim myCells9 As Range
Dim myCells10 As Range
Dim Total1 As Range


'
    Range("A1:G27").Select
    Range(Selection, Selection.End(xlDown)).Select
        Set myCells1 = Selection
    Range("H1:N1").Select
    Range(Selection, Selection.End(xlDown)).Select
        Set myCells2 = Selection
    Range("O1:U1").Select
    Range(Selection, Selection.End(xlDown)).Select
        Set myCells3 = Selection
    Range("V1:AB1").Select
    Range(Selection, Selection.End(xlDown)).Select
        Set myCells4 = Selection
    Range("AC1:AI1").Select
    Range(Selection, Selection.End(xlDown)).Select
        Set myCells5 = Selection
    Range("AJ1:AP1").Select
    Range(Selection, Selection.End(xlDown)).Select
        Set myCells6 = Selection
    Range("AQ1:AW1").Select
    Range(Selection, Selection.End(xlDown)).Select
        Set myCells7 = Selection
    Range("AX1:BD1").Select
    Range(Selection, Selection.End(xlDown)).Select
        Set myCells8 = Selection
    Range("BE1:BK1").Select
    Range(Selection, Selection.End(xlDown)).Select
        Set myCells9 = Selection
    Range("BM1:BR14").Select
        Set myCells10 = Selection
    'Set Total1 = .Range(myCells1, myCells2, myCells3, myCells4, myCells5, myCells6, myCells7, myCells8, myCells9, myCells10)
    'ActiveSheet.PageSetup.PrintArea = Range(Total1).Address
    With ActiveSheet
        .PageSteup.PrintArea = Union(myCells1, myCells2, myCells3, myCells4, myCells5, myCells6, myCells7, myCells8, myCells9, myCells10).Address
    End With
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
The Union statement isn't the problem, a typo is.
VBA Code:
  .PageSteup.PrintArea = Union(myCells1, myCells2, myCells3, myCells4, myCells5, myCells6, myCells7, myCells8, myCells9, myCells10).Address
Change it to .PageSetup and it works.
 
Upvote 0
Sometimes small mistakes lead to mysterious results. You are welcome!
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,793
Members
449,048
Latest member
greyangel23

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