Using the With Statement

You can use the With statement to eliminate the need for repetitive object references, without setting an explicit object variable. The example in the preceding section could be rewritten as follows, using the With statement. This example calls the Workbooks method and the Sheets method only once each.


With Workbooks(1).Sheets(1)
    .Range("c5").Value = 10
    .Range("d10").Value = 12
End With

Using the With statement eliminates the need for the intermediate variable used in the example in the preceding section; otherwise, this code is the same as in that example.