VBA With

With Statement in Excel VBA

With statement is used in VBA to access to all the properties and methods of a mentioned object. We need to supply which VBA objectVBA ObjectThe “Object Required” error, also known as Error 424, is a VBA run-time error that occurs when you provide an invalid object, i.e., one that is not available in VBA's object hierarchy.read more we are referring to first then close the With statement with End With, then inside this statement. We can perform all the property changes and methods of the mentioned object.

Below is the syntax of With Statement in VBA.

With [OBJECT]

     [Code… What needs to be done?]

End With

The object is nothing but a range of cells or cells we are referring to, and then we can change the properties and perform all the methods associated with that specific cell or cells.

How to Use With Statement in Excel VBA?

Below are some examples to use With Statement in Excel VBA.

You can download this VBA With Excel Template here – VBA With Excel Template

Example #1

Assume you have a certain value in A1 cell, I have entered the text as “Excel VBA” in cell A1.

Excel VBA With Statement Example 1.1

Now for this cell, I need to do some tasks, i.e., formatting in excelFormatting In ExcelFormatting is a useful feature in Excel that allows you to change the appearance of the data in a worksheet. Formatting can be done in a variety of ways. For example, we can use the styles and format tab on the home tab to change the font of a cell or a table.read more.

I want to change the font size, font name, and interior color, insert a border, etc.… Typical what we do is we first refer the cell by using VBA RANGE object.

Code:

Sub With_Example1()

    Range ("A1")

End Sub
Excel VBA With Statement Example 1.2

Now to change font size, we access the “font” property of this cell.

Excel VBA With Statement Example 1.3

Under FONT property, we access the Size property and enter size by putting an equal sign.

Code:

Sub With_Example1()

    Range("A1").Font.Size = 15

End Sub
Excel VBA With Statement Example 1.4

Now similarly, we do other formatting tasks, as shown below.

Code:

Sub With_Example1()

    Range("A1").Font.Size = 15
    Range("A1").Font.Name = "Verdana"
    Range("A1").Interior.Color = vbYellow
    Range("A1").Borders.LineStyle = xlContinuous

End Sub
Excel VBA With Statement Example 1.5

This will do all the mentioned tasks, but if you look at the code to perform each every formatting activity, we have supplied the cell address every time. This makes the code look lengthy and time-consuming.

Now we will use VBA With a statement to reduce the entry of cell address every time. Open WITH statement in Excel VBA and supply cell address.

Code:

Sub With_Example1()

    With Range("A1")

End Sub
Excel VBA With Statement Example 1.6

Inside the With statement, put a dot to see all the properties and methods of cell A1.

Excel VBA With Statement Example 1.7

Now first formatting activity is changing font size, so access FONT and under this access SIZE property.

Code:

Sub With_Example1()

    With Range("A1")
         .Font.Size = 15

End Sub
Example 1.8

Similarly, supply other formatting codes and close VBA With Statement.

Code:

Sub With_Example1()

    With Range("A1")
         .Font.Size = 15
         .Font.Name = "Verdana"
         .Interior.Color = vbYellow
         .Borders.LineStyle = xlContinuous
    End With

End Sub
Example 1.9

Run the code to see all the formatting in the mentioned object, i.e., cell A1.

VBA With Example1 - Output

So, all the formatting applied to the cell. Look how cool this technique is.

Example #2

For example, if you want to change all the properties related to the font, you can mention the cell and FONT property.

Code:

Sub With_Example2()

    With Range("A1").Font

    End With

End Sub
Example 2.1

Inside the VBA With Statement, we can see the IntelliSense list. It will show properties and methods related to FONT property only.

Example 2.2

We can perform any set of activities with this now.

Code:

Sub With_Example2()

    With Range("A1").Font
         .Bold = True 'Font will be Bold
         .Color = vbAlias 'Font color will be Alias
         .Italic = True 'Font will be italic style
         .Size = 20 ' Font size will be 20
         .Underline = True 'Font will be underlined
    End With

End Sub
Example 2.3

The result of this will be as shown below.

VBA With Example 2- Output

Example #3

The below code will access only cell border-related properties.

Code:

Sub With_Example3()

    With Range("B2").Borders
         .Color = vbRed 'Border color will be red
         .LineStyle = xlContinuous 'Full border
         .Weight = xlThick 'Thick border
    End With

End Sub
Example 3.1

The result of this code is as follows.

VBA With Example 3 - Output

Things to Remember

  • With statement is used to minimize the code.
  • We need to supply object first for With statement.
  • Once the specific object is supplied, we can access only that object’s properties and methods.

Recommended Articles

This has been a guide to VBA With. Here we discuss how to use With…End With Statement in Excel VBA along with examples and downloadable excel sheet. You can learn more about VBA from the following articles –

  • 35+ Courses
  • 120+ Hours
  • Full Lifetime Access
  • Certificate of Completion
LEARN MORE >>