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 object 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 cell 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.
Assume you have certain value in A1 cell, I have entered the text as “Excel VBA” in cell A1.
Now for this cell, I need to do some tasks i.e. formatting.
I want to change 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.
Sub With_Example1() Range ("A1") End Sub
Now to change font size we access “font” property of this cell.
Under FONT property we access the Size property and enter size by putting an equal sign.
Sub With_Example1() Range("A1").Font.Size = 15 End Sub
Now similarly we do other formatting tasks as shown below.
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
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 statement to reduce the entry of cell address every time. Open WITH statement in Excel VBA and supply cell address.
Sub With_Example1() With Range("A1") End Sub
Inside the With statement put a dot to see all the properties and methods of cell A1.
Now first formatting activity is changing font size, so access FONT and under this access SIZE property.
Sub With_Example1() With Range("A1") .Font.Size = 15 End Sub
Similarly, supply other formatting codes and close VBA With Statement.
Sub With_Example1() With Range("A1") .Font.Size = 15 .Font.Name = "Verdana" .Interior.Color = vbYellow .Borders.LineStyle = xlContinuous End With End Sub
Run the code to see all the formatting in the mentioned object i.e. cell A1.
So, all the formatting applied to the cell. Look how cool this technique is.
For example, if you want to change all the properties related to the font you can mention the cell and FONT property.
Sub With_Example2() With Range("A1").Font End With End Sub
Inside the vba With Statement, we can see IntelliSense list it will show properties and methods related to FONT property only.
We can perform any set of activity with this now.
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
The result of this will be as shown below.
Below code will access only cell border-related properties.
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
The result of this code is as follows.
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.
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 –