Documentation
-
Office VBA reference topic
-
Office VBA reference topic
-
Using For Each...Next statements (VBA)
Office VBA reference topic
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The With statement lets you specify an object or user-defined type once for an entire series of statements. With statements make your procedures run faster and help you avoid repetitive typing.
The following example fills a range of cells with the number 30, applies bold formatting, and sets the interior color of the cells to yellow.
Sub FormatRange()
With Worksheets("Sheet1").Range("A1:C10")
.Value = 30
.Font.Bold = True
.Interior.Color = RGB(255, 255, 0)
End With
End Sub
With statements may also be nested. The following example inserts a formula into cell A1, and then formats the font.
Sub MyInput()
With Workbooks("Book1").Worksheets("Sheet1").Cells(1, 1)
.Formula = "=SQRT(50)"
With .Font
.Name = "Arial"
.Bold = True
.Size = 8
End With
End With
End Sub
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.
Documentation
Office VBA reference topic
Office VBA reference topic
Using For Each...Next statements (VBA)
Office VBA reference topic
Training
Module
Build Office Add-ins for Word - Training
This module walks through development of Office Add-ins for Microsoft Word.