What is the syntax to use a With statement?

With <object expression>
 .<statements>
End Selection

Use these statements when you need to set many properties for the same object. Improves code readability

‘example without with statement
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlLeft
Selection.Orientation = 0
Selection.WrapText = false
Selection.MergeCells=true

‘example using a with statement
With Selection
 .HorizontalAlignment = xlCenter
 .VerticalAlignment = xlLeft
 .Orientation = 0
 .WrapText = false
 .Mergecells=true
End With
Advertisements