Refer to other objects within a "With" block?

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
Eg:

Code:
With theSheet.[COLOR=red]PageSetup[/COLOR]
[COLOR=#ff0000][/COLOR] 
    If a2 = 1 Then PrintArea = "A1:E21": .Zoom = 150
    If a2 = 2 Then .PrintArea = "A1:E43"
    If a2 = 3 Then .PrintArea = "A1:K21"
    .[COLOR=red]PrintOut[/COLOR]
[COLOR=#ff0000][/COLOR] 
'clears settings after printing
 
    .PageSetup.PrintArea = False
    .PageSetup.CenterVertically = False
    .PageSetup.Zoom = 91
End With

.Printout applies to a worksheet object, not the pagesetup object.

Is there a statement that allows a temporary break from the conditions of a with-block inside itself?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
You can always refer to any object inside a With block. Just use the object name and then .method. So, if you want to refer to the worksheet's PrintOut method use ActiveSheet.PrintOut or Workbooks(1).Worksheets(1).PrintOut or whatever else you want to do.

Just remember that all that the With block does is that *if* you start a property or method reference with a dot, it refers to the object in the With statement. *Nothing* stops you from referring to any other object inside the block.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,728
Members
452,939
Latest member
WCrawford

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