With and ScreenUpdating: shorter and faster macros
The longer a macro gets, the more often the same object opens every line: Worksheets("Data").Range(…), Worksheets("Data").Cells(…). With turns that into a block where a dot is enough. And ScreenUpdating stops Excel from redrawing the screen after every single change.
In short: With Worksheets("Data") … End With: in between, anything that starts with a dot (.Range("A1"), .Cells(i, 2)) refers to that sheet. Application.ScreenUpdating = False at the start and = True at the end makes long macros faster.
What you need to know: formatting cells and working with worksheets.
Try it right now
The header row gets three formats at once. Put the range in the With line once, then the three properties below it, each with a dot. Check compares the formatting and tests with an extra column.
Task Make the table's header row bold, give it the fill color RGB(217, 225, 242) and center it (xlCenter). Write the macro FormatHeader with a With block for the first row of the table. Sometimes an extra column is added on the right, which makes the header wider.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Rep | Region | Sales | Target |
| 2 | Emma Clarke | South | 18,400 | 20,000 |
| 3 | Jake Turner | North | 22,150 | 20,000 |
| 4 | Mia Johnson | West | 9,800 | 15,000 |
| 5 |
How With works
Without With, the object appears on every line:
Worksheets("Report").Range("A1").Value = "Monthly report"
Worksheets("Report").Range("A1").Font.Bold = True
Worksheets("Report").Range("A1").Font.Size = 14
With With, you name it once:
With Worksheets("Report").Range("A1")
.Value = "Monthly report"
.Font.Bold = True
.Font.Size = 14
End With
| Part | Meaning |
|---|---|
With object | Start of the block. The object can be a range, a sheet, a font (.Font) or any other object. |
.Property | Anything that starts with a dot belongs to the object from the With line. |
| no dot | Ordinary code. Range("A1") without a dot means the active sheet, as always. |
End With | End of the block. Without it, VBA reports "Compile error: Expected End With" before the macro starts. |
With blocks can be nested. Inside the inner block, the dot refers to the inner object:
With Range("A1:D1")
.Interior.Color = RGB(217, 225, 242)
With .Font
.Bold = True
.Size = 12
End With
End With
With for a whole sheet
With pays off most when a macro works with a sheet that is not active. Instead of writing Worksheets("Data"). in front of everything, you open a block. Put the dot in front of Rows.Count too, so that really everything belongs to the right sheet.
With Worksheets("Data")
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 2 To lastRow
total = total + .Cells(i, 2).Value * .Cells(i, 3).Value
Next i
End With
ScreenUpdating: faster without the screen
Normally Excel shows every change right away. For a macro that writes hundreds of cells, deletes rows or switches sheets, that takes a lot of time. Microsoft puts it this way: with screen updating off you will not see what the macro is doing, but it will run faster. And: set it back to True when the macro ends.
Sub FillReport()
Application.ScreenUpdating = False
' … the actual work …
Application.ScreenUpdating = True
End Sub
On short macros you will not notice a difference. On loops over many rows, especially with Delete, Insert or formatting, it is worth it. The same idea applies to Application.Calculation = xlCalculationManual: Excel then holds off recalculating formulas until you set xlCalculationAutomatic again at the end.
Task The Report sheet is active, and the order lines are on the Data sheet. The macro FillReport loops over all lines to total the sales (quantity times price, added up) and writes it to B1 of the report, with the number of lines in B2. Refer to the Data sheet with a With block, and turn screen updating off at the start and back on at the end.
Report
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Total sales | |||
| 2 | Lines | |||
| 3 | ||||
| 4 | ||||
| 5 |
Data
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Item | Qty | Price | |
| 2 | Lever arch file | 40 | 2.49 | |
| 3 | Copy paper | 25 | 4.95 | |
| 4 | Ballpoint pen | 120 | 0.89 | |
| 5 | Sticky notes | 60 | 1.75 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
Find the bug
The most common problem with With is a missing dot. The macro then runs without any message but works on the wrong sheet.
Task Find the bug: the Entry sheet holds customer, invoice number and amount. The macro PostEntry should append them to the bottom of the list on the List sheet and then clear the entry. Instead, it overwrites an invoice in the middle of the list. A single character is missing.
Entry
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Customer | Lakeside Hotel | ||
| 2 | Invoice | 1046 | ||
| 3 | Amount | 640.00 | ||
| 4 | ||||
| 5 |
List
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Customer | Invoice | Amount | |
| 2 | Carter Bakery | 1041 | 480.00 | |
| 3 | Peters Motors | 1042 | 1,250.00 | |
| 4 | Green Dental | 1043 | 315.50 | |
| 5 | Harris & Sons | 1044 | 96.00 | |
| 6 | Miller Print | 1045 | 2,100.00 |
This exercise is part of Premium.
Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.
See Premium Already Premium? Log in
The first exercise in this lesson is free.
In real Excel
If a macro stops with an error before it reaches ScreenUpdating = True, that line never runs. How to make sure it runs anyway is covered in Error handling.
When stepping through with F8, a frozen screen gets in the way: you want to see what happens. Comment out the ScreenUpdating = False line while debugging (an apostrophe in front) and remove the apostrophe once everything works. More on stepping through code in Debugging.
Common mistakes
Missing dot
Inside the With block for the List sheet, target = Cells(Rows.Count, 1).End(xlUp).Row + 1 has no dot. Cells then refers to the active sheet, not List, and the macro writes into a row that is already taken. In the exercise, Check finds the new customer in List!A4 where Green Dental belongs.
A dot where none belongs
It also goes wrong the other way round: .Range("B1") with a dot reads from the With sheet, although the values should come from the active sheet. For each line in the block, ask yourself which sheet it means.
Property without a dot
Font.Bold = True without a dot inside the block is not a property of any object to VBA. The exercise reports "Run-time error '424': Object required".
Missing End With
Every With block needs its End With, including the inner one when nesting. Otherwise you get "Compile error: Expected End With" before the macro starts.
ScreenUpdating never switched back on
If there is no Application.ScreenUpdating = True at the end, Microsoft explicitly recommends adding it. Put the line right before End Sub and you will not forget it.
FAQ
Does With make a macro faster?
Mainly, With saves typing and makes clear which object the block works on. VBA works out the object in the With line once and uses it for every line with a dot. What really speeds a macro up is ScreenUpdating and writing whole ranges at once.
Can I use another sheet inside a With block?
Yes, just with its full name, such as Worksheets("Customers").Range("A1"). Only what starts with a dot belongs to the With object.
Can I use With with a variable?
Yes: With found.Resize(1, 5) or With ws when ws holds a sheet. Any object works.
What about DisplayAlerts?
Application.DisplayAlerts = False suppresses prompts such as the one when deleting a sheet. It belongs back on True at the end as well. More in Worksheets.