Refer to Multiple Ranges
By using the appropriate method, you can easily refer to multiple ranges. Use the Range and Union methods to refer to any group of ranges. Use the Areas property to refer to the group of ranges selected on a worksheet.
Using the Range Property
You can refer to multiple ranges with the Range property by inserting commas between two or more references. The following example clears the contents of three ranges on Sheet1.
Sub ClearRanges() Worksheets("Sheet1").Range("C5:D9,G9:H16,B14:D18"). _ ClearContents End Sub
Named ranges make it easier to use the Range property to work with multiple ranges. The following example works when all three named ranges are on the same sheet.
Sub ClearNamed() Range("MyRange, YourRange, HisRange").ClearContents End Sub
Using the Union Method
You can combine multiple ranges into one Range object by using the Union method. The following example creates a Range object called
myMultipleRange, defines it as the ranges A1:B2 and C3:D4, and then formats the combined ranges as bold.
Sub MultipleRange() Dim r1 As Range, r2 As Range, myMultipleRange As Range Set r1 = Sheets("Sheet1").Range("A1:B2") Set r2 = Sheets("Sheet1").Range("C3:D4") Set myMultipleRange = Union(r1, r2) myMultipleRange.Font.Bold = True End Sub
Using the Areas Property
You can use the Areas property to refer to the selected range or to the collection of ranges in a multiple-area selection. The following procedure counts the areas in the selection. If there is more than one area, a warning message is displayed.
Sub FindMultiple() If Selection.Areas.Count > 1 Then MsgBox "Cannot do this to a multiple selection." End If End Sub
Support and feedback
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.