
So yeah guys, this is how we use With - End With block in VBA. Experienced programmers use both methods at suitable situations. If you will be using child objects then use the previous approach. So when you know that you are going to use multiple methods and properties from an object, give a fully qualified name at the beginning. With ThisWorkbook.Sheets("Shee2").Range("A1:A10").Font
#Comment a block in vba in excel for mac code#
If I want to make some changes with the font of range A1:A10 of sheet2 in the code containing the workbook then we should use a fully qualified block. 'The below code will generate error as copy and clear methods does not belong to font class. Once you are a child, you can't access parent specific properties. When you start inner with, the object should be written with the preceding dot operator. We can refer to the outer with as object with and inner With as Child With. The inner with block should refer to an object that is a sub member of the outer object. The above code can be written like this too: Sub test() Then we used Range.Font several times to work with fonts. In the above example we used a range object to select it. We can have a block within another block.

The reference lock releases when VBA reads the End With statement. So whenever we write dot (.) operator, VBA lists all the members of this range class which will affect only object Range("A1:A10") or any object you mentioned. Well, when we write With Range("A1:A10"), vba locks its reference to the object range("A1:A10"). If you have created an object of Outlook Mail then you can use that object to initialize all it's properties and use methods. The alternative to do this is to use a With block: The below code does the same as the above code but faster. This decreases the processing speed and increases work overhead to VBA programmers. Then the dot operator accesses its properties. You can notice that to do every operation with range A1:A10, we have to write it every time. Then it copies the range A1:A10 to range B1:B10 of the same sheet. Underlines the text in range with double underlines. Changes the interior color of range to color index 8.

Range("A1:A10").Font.Underline = xlUnderlineStyleDouble How would I do it generally? Probably like this: Sub test() I want to select this range, change it's fill colors, font style etc. Let's say I want to do several changes to range A2:A10. So now that we know the syntax of With - End With block, let's see its usage. We will see examples later in this article. The With - End With block in VBA is used to tell VBA that we are going to use the given object and it should consider the properties of the given object only once we use dot operator.
