PSS ID Number: 120198
Article Last Modified on 9/11/2002
Method Arguments
------------------------------------------
Activate none
Cells rowIndex, columnIndex
Application.Goto reference, scroll
Offset rowOffset, columnOffset
Range cell1
cell1, cell2
Resize rowSize, columnSize
Select none
Sheets index (or sheetName)
Workbooks index (or bookName)
End direction
CurrentRegion none
The examples in this article use the properties in the following table.
Property Use --------------------------------------------------------------------- ActiveSheet to specify the active sheet ActiveWorkbook to specify the active workbook Columns.Count to count the number of columns in the specified item Rows.Count to count the number of rows in the specified item Selection to refer to the currently selected range
ActiveSheet.Cells(5, 4).Select
-or-
ActiveSheet.Range("D5").Select
Application.Goto ActiveWorkbook.Sheets("Sheet2").Cells(6, 5)
-or-
Application.Goto ActiveWorkbook.Sheets("Sheet2").Range("E6")
Or, you can activate the worksheet, and then use method 1 above to select
the cell:
Sheets("Sheet2").Activate
ActiveSheet.Cells(6, 5).Select
Application.Goto _
Workbooks("BOOK2.XLS").Sheets("Sheet1").Cells(7, 6)
-or-
Application.Goto _
Workbooks("BOOK2.XLS").Sheets("Sheet1").Range("F7")
Or, you can activate the worksheet, and then use method 1 above to
select the cell:
Workbooks("BOOK2.XLS").Sheets("Sheet1").Activate
ActiveSheet.Cells(7, 6).Select
ActiveSheet.Range(Cells(2, 3), Cells(10, 4)).Select
ActiveSheet.Range("C2:D10").Select
ActiveSheet.Range("C2", "D10").Select
Application.Goto _
ActiveWorkbook.Sheets("Sheet3").Range("D3:E11")
Application.Goto _
ActiveWorkbook.Sheets("Sheet3").Range("D3", "E11")
Or, you can activate the worksheet, and then use method 4 above to select
the range:
Sheets("Sheet3").Activate
ActiveSheet.Range(Cells(3, 4), Cells(11, 5)).Select
Application.Goto _
Workbooks("BOOK2.XLS").Sheets("Sheet1").Range("E4:F12")
Application.Goto _
Workbooks("BOOK2.XLS").Sheets("Sheet1").Range("E4", "F12")
Or, you can activate the worksheet, and then use method 4 above to select
the range:
Workbooks("BOOK2.XLS").Sheets("Sheet1").Activate
ActiveSheet.Range(Cells(4, 5), Cells(12, 6)).Select
Range("Test").Select
Application.Goto "Test"
Application.Goto Sheets("Sheet1").Range("Test")
Or, you can activate the worksheet, and then use method 7 above to select
the named range:
Sheets("Sheet1").Activate
Range("Test").Select
Application.Goto _
Workbooks("BOOK2.XLS").Sheets("Sheet2").Range("Test")
Or, you can activate the worksheet, and then use method 7 above to select
the named range:
Workbooks("BOOK2.XLS").Sheets("Sheet2").Activate
Range("Test").Select
ActiveCell.Offset(5, -4).SelectTo select a cell that is two rows above and three columns to the right of the active cell, you can use the following example:
ActiveCell.Offset(-2, 3).SelectNOTE: An error will occur if you attempt to select a cell that is "off the worksheet." 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.
ActiveSheet.Cells(7, 3).Offset(5, 4).Select
ActiveSheet.Range("C7").Offset(5, 4).Select
ActiveSheet.Range("Test").Offset(4, 3).Select
If the named range is on another (not the active) worksheet, activate that
worksheet first, and then select the range using the following example:
Sheets("Sheet3").Activate
ActiveSheet.Range("Test").Offset(4, 3).Select
Range("Database").Select
Selection.Resize(Selection.Rows.Count + 5, _
Selection.Columns.Count).Select
Range("Database").Select
Selection.Offset(4, 3).Resize(Selection.Rows.Count + 2, _
Selection.Columns.Count + 1).Select
Application.Union(Range("Test"), Range("Sample")).Select
Note that both ranges must be on the same worksheet for this example to
work. 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
Application.Intersect(Range("Test"), Range("Sample")).Select
Note that both ranges must be on the same worksheet for this example to
work.
Examples 17-21 in this article refer to the following example set of data.
Each example states the range of cells in the sample data that would be
selected.
A1: Name B1: Sales C1: Quantity A2: a B2: $10 C2: 5 A3: b B3: C3: 10 A4: c B4: $10 C4: 5 A5: B5: C5: A6: Total B6: $20 C6: 20
ActiveSheet.Range("a1").End(xlDown).Select
When this code is used with the sample table, cell A4 will be selected.
ActiveSheet.Range("a1").End(xlDown).Offset(1,0).Select
When this code is used with the sample table, cell A5 will be selected.
ActiveSheet.Range("a1", ActiveSheet.Range("a1").End(xlDown)).Select
-or-
ActiveSheet.Range("a1:" & ActiveSheet.Range("a1"). _
End(xlDown).Address).Select
When this code is used with the sample table, cells A1 through A4 will be
selected.
ActiveSheet.Range("a1",ActiveSheet.Range("a16384").End(xlUp)).Select
-or-
ActiveSheet.Range("a1:" & ActiveSheet.Range("a16384"). _
End(xlUp).Address).Select
When this code is used with the sample table, it will select cells A1
through A6.
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(16384, lastCol).End(xlUp).Row
ActiveSheet.Range("a1", _
ActiveSheet.Cells(lastRow, lastCol)).Select
-or-
lastCol = ActiveSheet.Range("a1").End(xlToRight).Column
lastRow = ActiveSheet.Cells(16384, lastCol).End(xlUp).Row
ActiveSheet.Range("a1:" & _
ActiveSheet.Cells(lastRow, lastCol).Address).Select
A1: 1 B1: 1 C1: 1 D1: 1 A2: 2 B2: 2 C2: 2 D2: 2 A3: 3 B3: 3 C3: 3 D3: 3 A4: B4: 4 C4: 4 D4: 4 A5: B5: 5 C5: 5 D5: A6: B6: C6: 6 D6:
StartRange = "A1" EndRange = "C1" Set a = Range(StartRange, Range(StartRange).End(xlDown)) Set b = Range(EndRange, Range(EndRange).End(xlDown)) Union(a,b).SelectWhen this code is used with the sample table, cells A1:A3 and C1:C6 will be selected.
ActiveSheet.Range("D5").Select
you can use:
Range("D5").Select
Application.Goto Sheets("Sheet1").Range( _
Sheets("Sheet1").Range(Sheets("Sheet1").Cells(2, 3), _
Sheets("Sheet1").Cells(4, 5)))
ActiveWorkbook.Sheets("Sheet1").Activate
you can use:
ActiveWorkbook.Sheets(myVar).Activatewhere the value of myVar is "Sheet1".
Additional query words: XL97 howto contiguous how to discontiguous non-contiguous nonadjacent non-adjacent vba
Keywords: kbdtacode kbhowto kbProgramming kbualink97 KB120198
Technology: kbExcel500 kbExcel500aMac kbExcel500c kbExcel500Mac kbExcel500NT kbExcel95 kbExcel95Search kbExcel97Search kbExcel98 kbExcel98Search kbExcelMacsearch kbExcelSearch kbExcelWinSearch kbHWMAC kbOSMAC kbVBASearch kbZNotKeyword3 kbZNotKeyword6