[vba] dynamic printarea

Antman404

New Member
Joined
May 26, 2020
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
Hello again
i got this code
y1.PageSetup.PrintArea = Union(PA(1), PA(2), PA(3), PA(4), PA(5), PA(6), PA(7)).Address

problem is, if one or more of PA(#) contain no range, the union gives an error

now i need something to exclude the empty PA(#)'s.
i tried the following
VBA Code:
Dim PA As Object
Set PA = CreateObject("Scripting.Dictionary")

...CODE WHERE PA(#) GETS A RANGE OR NOT...

For v = 1 To 6

If PA(v) Is Not Nothing Then

y1.PageSetup.PrintArea = Union(PA(v - 1), PA(v)).Address

End If

Next v
But it does only work with the last two PA(#).

PA(1) and PA(2) will always have a Range, so there will be at no time all of them empty
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Perhaps something like this will work for you ...
VBA Code:
    Dim PA As Object
    Set PA = CreateObject("Scripting.Dictionary")

    ...CODE WHERE PA(#) GETS A RANGE OR NOT...

    Dim r As Range
    
    For v = 0 To 5
        If Not PA(v) Is Nothing Then
            If r Is Nothing Then
                Set r = PA(v)
            Else
                Set r = Union(r, PA(v))
            End If
        End If
    Next v
    
    y1.PageSetup.PrintArea = r.Address
 
Upvote 0
Solution
You are welcome and thanks for letting me know.
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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