How to Select Cells and Ranges in Excel Visual Basic
Use the Visual Basic methods listed in this table: ,Use the Visual Basic properties in this table:, Use either of the following examples to select cell E6 on the active worksheet: ActiveSheet.Cells(6, 5).Select -or- ActiveSheet.Range("E6").Select...
Step-by-Step Guide
-
Step 1: Use the Visual Basic methods listed in this table:
The first example shown above will return an error if the active cell is in columns A through D, since moving four columns to the left would take the active cell to an invalid cell address. ,,,,, Note also that the Union method does not work across sheets.
For example, this line works fine Set y = Application.Union(Range("Sheet1!A1:
B2"), Range("Sheet1!C3:
D4")) but this line Set y = Application.Union(Range("Sheet1!A1:
B2"), Range("Sheet2!C3:
D4")) returns the error message:
Union method of application class failed ,, Each example states the range of cells in the sample data that would be selected.,,, _ End(xlDown).Address).Select When this code is used with the sample table, cells A1 through A4 will be selected. , _ End(xlUp).Address).Select When this code is used with the sample table, it will select cells A1 through A6. , The range selected by the CurrentRegion method is an area bounded by any combination of blank rows and blank columns.
The following is an example of how to use the CurrentRegion method:
ActiveSheet.Range("a1").CurrentRegion.Select This code will select cells A1 through C4.
Other examples to select the same range of cells are listed below:
ActiveSheet.Range("A1"
_ #*ActiveSheet.Range("A1").End(xlDown).End(xlToRight)).Select or ActiveSheet.Range("A1:" & _ ActiveSheet.Range("a1").End(xlDown).End(xlToRight).Address).Select In some instances, you may want to select cells A1 through C6.
In this example, the CurrentRegion method will not work because of the blank line on Row
5.
The following examples will select all of the cells: lastCol = ActiveSheet.Range("A1").End(xlToRight).Column lastRow = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row ActiveSheet.Range("A1"
ActiveSheet.Cells(lastRow, lastCol)).Select or lastCol = ActiveSheet.Range("a1").End(xlToRight).Column lastRow = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row ActiveSheet.Range("a1:" & _ ActiveSheet.Cells(lastRow, lastCol).Address).Select ,, Herewith is an image of a non-contiguous selection in Excel. , To select, you would type aCell.
Select before the End If.
See the article How to Use "Find" in Excel VBA Macros for exactly that modification to the code herewith supplied.,, Record a dummy macro by choosing Developer on the Ribbon and then hitting the Record icon and just selecting cell A1, and then Stop Recording.,,,,,, See the aforementioned article regarding other types.
UserRange.Clear UserRange.Select Canceled:
End Sub ,,, -
Step 2: Use the Visual Basic properties in this table:
-
Step 3: Use either of the following examples to select cell E6 on the active worksheet: ActiveSheet.Cells(6
-
Step 4: 5).Select -or- ActiveSheet.Range("E6").Select
-
Step 5: Use either of the following examples to select cell E6 on another worksheet in the same workbook: Application.Goto ActiveWorkbook.Sheets("Sheet2").Cells(6
-
Step 6: 5) -or- Application.Goto (ActiveWorkbook.Sheets("Sheet2").Range("E6")) Or
-
Step 7: activate the worksheet
-
Step 8: then use the method above to select the cell: Sheets("Sheet2").Activate ActiveSheet.Cells(6
-
Step 9: 5).Select
-
Step 10: Use either of the following examples: to select cell A2 on a worksheet in a different workbook: Application.Goto Workbooks("BOOK2.XLS").Sheets("Sheet1").Cells(2
-
Step 11: 1) or Application.Goto Workbooks("BOOK2.XLS").Sheets("Sheet1").Range("A2") Or
-
Step 12: activate the worksheet
-
Step 13: then use method 1 above to select the cell: Workbooks("BOOK2.XLS").Sheets("Sheet1").Activate ActiveSheet.Cells(2
-
Step 14: 1).Select
-
Step 15: Use any of the following examples to select the range C1:D6 on the active worksheet: ActiveSheet.Range(Cells(1
-
Step 16: Cells(6
-
Step 17: 4)).Select ActiveSheet.Range("C1:D6").Select ActiveSheet.Range("C1"
-
Step 18: "D6").Select
-
Step 19: Use either of the following examples to select the range C3:E11 on another worksheet in the same workbook: Application.Goto ActiveWorkbook.Sheets("Sheet3").Range("C3:E11") Application.Goto ActiveWorkbook.Sheets("Sheet3").Range("C3"
-
Step 20: "E11") Or
-
Step 21: activate the worksheet
-
Step 22: then use the method above to select the range: Sheets("Sheet3").Activate ActiveSheet.Range(Cells(3
-
Step 23: Cells(11
-
Step 24: 5)).Select
-
Step 25: Use either of the following examples to select the range E12:F12 on a worksheet in a different workbook: Application.Goto Workbooks("BOOK2.XLS").Sheets("Sheet1").Range("E12:F12") Application.Goto _ Workbooks("BOOK2.XLS").Sheets("Sheet1").Range("E12"
-
Step 26: "F12") Or
-
Step 27: activate the worksheet
-
Step 28: then use the method above to select the range: Workbooks("BOOK2.XLS").Sheets("Sheet1").Activate ActiveSheet.Range(Cells(12
-
Step 29: Cells(12
-
Step 30: 6)).Select
-
Step 31: Use either of the following examples to select the named range "Test1" on the active worksheet: Range("Test1").Select Application.Goto "Test1"
-
Step 32: Use the following example to select the named range "Test2" on another worksheet in the same workbook: Application.Goto Sheets("Sheet3").Range("Test2") Or
-
Step 33: activate the worksheet
-
Step 34: then use the method above to select the named range: Sheets("Sheet3").Activate Range("Test2").Select
-
Step 35: Use the following example to select the named range "Test3" on a worksheet in a different workbook: Application.Goto _ Workbooks("BOOK2.XLS").Sheets("Sheet4").Range("Test3") Or
-
Step 36: activate the worksheet
-
Step 37: then use the method above to select the named range: Workbooks("BOOK2.XLS").Sheets("Sheet4").Activate Range("Test3").Select
-
Step 38: Use the following example to select a cell that is three rows below and four columns to the left of the active cell: ActiveCell.Offset(3
-
Step 39: -4).Select Use the following example to select a cell that is one row above and three columns to the right of the active cell: ActiveCell.Offset(-1
-
Step 40: 3).Select Note An error will occur if you try to select a cell that is "off the worksheet."
-
Step 41: Use either of the following examples to select a cell that is five rows below and four columns to the right of cell C7: ActiveSheet.Cells(7
-
Step 42: 3).Offset(5
-
Step 43: 4).Select ActiveSheet.Range("C7").Offset(5
-
Step 44: 4).Select
-
Step 45: Use the following example to select a range of cells that is the same size as the named range "Test5" but that is shifted four rows down and three columns to the right: ActiveSheet.Range("Test5").Offset(4
-
Step 46: 3).Select If the named range is on another (not the active) worksheet
-
Step 47: activate that worksheet first
-
Step 48: and then select the range using the following example: Sheets("Sheet3").Activate ActiveSheet.Range("Test").Offset(4
-
Step 49: 3).Select
-
Step 50: Use the following example to select the named range "Database"
-
Step 51: then extend the selection by five rows: Range("Database").Select Selection.Resize(Selection.Rows.Count + 5
-
Step 52: _Selection.Columns.Count).Select
-
Step 53: Use the following example to select a range four rows below and three columns to the right of the named range "Database" and include two rows and one column more than the named range: Range("Database").Select Selection.Offset(4
-
Step 54: 3).Resize(Selection.Rows.Count + 2
-
Step 55: _Selection.Columns.Count +1).Select
-
Step 56: Use the following example to select the union (that is
-
Step 57: the combined area) of the two named ranges "Test" and "Sample
-
Step 58: ": Application.Union(Range("Test")
-
Step 59: Range("Sample")).Select Note that both ranges must be on the same worksheet for this example to work.
-
Step 60: Use the following example to select the intersection of the two named ranges "Nest" and "Eggs": Application.Intersect(Range("Nest")
-
Step 61: Range("Eggs")).Select Note that both ranges must be on the same worksheet for this example to work.
-
Step 62: The following five examples in this article refer to the following sample set of data.
-
Step 63: Use the following example to select the last cell in a contiguous column: ActiveSheet.Range("A1").End(xlDown).Select When this code is used with the sample table
-
Step 64: cell A4 will be selected.
-
Step 65: Use the following example to select the cell below a range of contiguous cells: #*ActiveSheet.Range("A1").End(xlDown).Offset(1
-
Step 66: 0).Select When this code is used with the sample table
-
Step 67: cell A5 will be selected.
-
Step 68: Use use one of the following examples to select a range of contiguous cells in a column: ActiveSheet.Range("A1"
-
Step 69: ActiveSheet.Range("a1").End(xlDown)).Select or ActiveSheet.Range("A1:" & ActiveSheet.Range("A1").
-
Step 70: Use one of the following examples to select a range of cells that are non-contiguous: ActiveSheet.Range("A1"
-
Step 71: ActiveSheet.Range("A65536").End(xlUp)).Select #** or ActiveSheet.Range("A1:" & ActiveSheet.Range("A65536").
-
Step 72: Use the CurrentRegion method in order to select a rectangular range of cells around a cell.
-
Step 73: Use the following sample table and macro example to select multiple non-contiguous columns of varying length:
-
Step 74: StartRange = "A1" EndRange = "C1" Set a = Range(StartRange
-
Step 75: Range(StartRange).End(xlDown)) Set b = Range(EndRange
-
Step 76: Range(EndRange).End(xlDown)) Union(a
-
Step 77: b).Select When this code is used with the sample table
-
Step 78: cells A1:A3 and C1:C6 will be selected.
-
Step 79: Below is a macro which builds cell references
-
Step 80: and although it does not select them
-
Step 81: it does perform other useful tasks with them.
-
Step 82: Set up a workbook with Sheet1 having in cells A1 to A60000 the values 1 to 60000.
-
Step 83: In the Excel Menu
-
Step 84: Preferences
-
Step 85: set the Ribbon Developer to checked or On
-
Step 86: so you can work with macros.
-
Step 87: Copy the macro below using Advanced Editing to a text processor like MS Word and do a REPLACE ALL for "#** " (w/o the quotes and with the trailing space) over all the entire selection.
-
Step 88: Copy the selection and paste it over your dummy macro in Excel by choosing the (VB) EDIT(OR) icon button under Developer on the Ribbon.
-
Step 89: Run the macro by choosing menuitem Tools .. Macros .. and the name of the macro
-
Step 90: Create a button by selecting the button icon to the right of the Record icon
-
Step 91: and assigning your macro to it
-
Step 92: then pressing the button on your worksheet to run the macro.
-
Step 93: Here's the macro to do the REPLACE ALL on (down to End Sub): Sub CommandButton2_Click() ‘ Assign this macro to a button you create Dim oSht As Worksheet Dim lastRow As Long
-
Step 94: i As Long Dim strSearch As String Dim aCell As Range Set oSht = Sheets("Sheet1") lastRow = oSht.Range("A" & Rows.Count).End(xlUp).Row strSearch = "10000" Set aCell = oSht.Range("A1:A" & lastRow).Find(What:=strSearch
-
Step 95: _ LookIn:=xlValues
-
Step 96: LookAt:=xlPart
-
Step 97: SearchOrder:=xlByRows
-
Step 98: _ SearchDirection:=xlNext
-
Step 99: MatchCase:=False
-
Step 100: SearchFormat:=False) If Not aCell Is Nothing Then MsgBox "Value Found in Cell " & aCell.Address End If Exit Sub End Sub
-
Step 101: Here's another macro
-
Step 102: instead acquiring a range from the user and erasing it prior to selecting it: Sub EraseRange() Dim UserRange as Range On Error GoTo Canceled Set UserRange = Application.InputBox _ (Prompt :="Range to erase:"
-
Step 103: _ Title :="Range Erase"
-
Step 104: _ Default :=Selection.Address
-
Step 105: _ Type := 8) ' Type 8 refers to a cell reference
-
Step 106: as a range object. '
-
Step 107: The user will select the range with the mouse and/or shift key and the absolute reference will appear in the Input Box prior to erasure.
-
Step 108: This sort of macro can also be used to fill a range with random numbers
-
Step 109: Watch as much of the following video as you care to
-
Step 110: and then come back here by using your ESC key or the BACK button in your browser while in YouTube.
Detailed Guide
The first example shown above will return an error if the active cell is in columns A through D, since moving four columns to the left would take the active cell to an invalid cell address. ,,,,, Note also that the Union method does not work across sheets.
For example, this line works fine Set y = Application.Union(Range("Sheet1!A1:
B2"), Range("Sheet1!C3:
D4")) but this line Set y = Application.Union(Range("Sheet1!A1:
B2"), Range("Sheet2!C3:
D4")) returns the error message:
Union method of application class failed ,, Each example states the range of cells in the sample data that would be selected.,,, _ End(xlDown).Address).Select When this code is used with the sample table, cells A1 through A4 will be selected. , _ End(xlUp).Address).Select When this code is used with the sample table, it will select cells A1 through A6. , The range selected by the CurrentRegion method is an area bounded by any combination of blank rows and blank columns.
The following is an example of how to use the CurrentRegion method:
ActiveSheet.Range("a1").CurrentRegion.Select This code will select cells A1 through C4.
Other examples to select the same range of cells are listed below:
ActiveSheet.Range("A1"
_ #*ActiveSheet.Range("A1").End(xlDown).End(xlToRight)).Select or ActiveSheet.Range("A1:" & _ ActiveSheet.Range("a1").End(xlDown).End(xlToRight).Address).Select In some instances, you may want to select cells A1 through C6.
In this example, the CurrentRegion method will not work because of the blank line on Row
5.
The following examples will select all of the cells: lastCol = ActiveSheet.Range("A1").End(xlToRight).Column lastRow = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row ActiveSheet.Range("A1"
ActiveSheet.Cells(lastRow, lastCol)).Select or lastCol = ActiveSheet.Range("a1").End(xlToRight).Column lastRow = ActiveSheet.Cells(65536, lastCol).End(xlUp).Row ActiveSheet.Range("a1:" & _ ActiveSheet.Cells(lastRow, lastCol).Address).Select ,, Herewith is an image of a non-contiguous selection in Excel. , To select, you would type aCell.
Select before the End If.
See the article How to Use "Find" in Excel VBA Macros for exactly that modification to the code herewith supplied.,, Record a dummy macro by choosing Developer on the Ribbon and then hitting the Record icon and just selecting cell A1, and then Stop Recording.,,,,,, See the aforementioned article regarding other types.
UserRange.Clear UserRange.Select Canceled:
End Sub ,,,
About the Author
Alice Davis
Experienced content creator specializing in cooking guides and tutorials.
Rate This Guide
How helpful was this guide? Click to rate: