VbaModulesBeginner

With statement

Groups repeated access to the same object so code is shorter and clearer.

Review the syntaxStudy the examplesOpen the coding app
With object
    .Property = value
End With

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Groups repeated access to the same object so code is shorter and clearer.

With is especially useful in Excel VBA because many macros repeatedly touch the same range, worksheet, or workbook object. It reduces repetitive object qualifiers and makes formatting or report-building blocks easier to read. It is best when one target object stays stable across several lines.

Quick reference

Syntax

With object
    .Property = value
End With

See it in practice

Examples

1

Format a report header block

With Worksheets("Report").Range("A1:C1")
    .Value = Array("Region", "Sales", "Margin")
    .Font.Bold = True
    .Interior.Color = RGB(217, 225, 242)
End With

With keeps repeated Range qualifiers out of the way while formatting a block.

Debug faster

Common Errors

1

LogicError

Cause: Assuming the dot-prefixed members refer to a different object after leaving the With block.

Fix: Keep object-specific operations inside the With block and avoid nested ambiguity.

Runtime support

Compatibility

Excel desktop VBA

Source: Microsoft Learn Office VBA reference

Common questions

Frequently Asked Questions

Groups repeated access to the same object so code is shorter and clearer.

LogicError: Keep object-specific operations inside the With block and avoid nested ambiguity.