Excel VBA Programming - With ... End With
Excel VBA Programming - With ... End With
End With
Home
Getting Started
8 part section >> With ... End With in Excel VBA
VBA Programming Variables
6 Part Section >>
Conditional Logic
If Statements
Else ... ElseIf
Conditional Operators
Logical Operators
VBA Practice 1
VBA Practice 2
In a previous lesson, you had code like this:
Select Case
VBA Practice 3 ActiveCell(1, 2).Value = "F"
With ... End With
Strings and String Functions ActiveCell(1, 2).HorizontalAlignment = xlCenter
8 part section >>
ActiveCell(1, 3).Value = "Terrible - needs attention"
Programming Loops
4 part section >> We were accessing various properties of the ActiveCell. In the code above, we have ActiveCell
Programming Arrays three times. However, you can speed up your subroutines by using the repeated object only once,
4 part section >> on the first line. You then type the property you need after a dot. The syntax is this:
Subs and Functions
With object
6 part section >>
Excel VBA and Text Files .property
2 part section >>
Excel VBA and User Forms
End With
5 part section >> As an example, suppose we want to change various aspects of the ActiveCell. We want to change
An Excel Picture Viewer Project the font name, the font size, the boldness, the italics, etc. We could do it like this:
12 part section >>
Excel VBA and Charts ActiveCell.Font.Bold = True
4 part section >>
ActiveCell.Font.Color = vbBlue
ActiveCell.Font.Name = "Arial"
A TreeView Project ActiveCell.Font.Size = 22
A 4 part section >> ActiveCell.Font.Italic = True
> BUY THE BOOK OF THIS COURSE <
But notice the repetition here. We've used ActiveCell.Font five times. By using a With Statement,
we can just type the ActiveCell.Font once. Like this:
With ActiveCell.Font
.Bold = True
.Color = vbBlue
.Name = "Arial"
.Size = 22
.Italic = True
End With
So you start with the word With. After a space, you type what it is you're trying to manipulate. We
want to manipulate the Font property of ActiveCell. The Font property has lots of properties of its
own. These ones:
Type a dot and then the name of the Font property in the above list that you want to change. The
equal sign and the value are used in the normal way. The whole thing end with the words End
With.
With Statements are quite intuitive, so we don't really need to say too much about them. But just
remember: if you're typing the same object over and over, you might do better to use a With …
End With statement.
OK, that's enough of Conditional Logic. We'll move and take a look at another variable type that's
quite common - As String. We'll do that in the next section below.
https://www.homeandlearn.org/with_end_with.html 1/2
6/22/2019 Excel VBA Programming - With ... End With
Lots more free online course here on our main Home and Learn site
https://www.homeandlearn.org/with_end_with.html 2/2