
#Visual basic for excel cells() function how to
How to Select a Cell Relative to Another (Not the Active) Cell To 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).SelectĪn error will occur if you try 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. To select a cell that is five rows below and four columns to the left of the active cell, you can use the following example: ActiveCell.Offset(5, -4).Select How to Select a Cell Relative to the Active Cell Or, you can activate the worksheet, and then use method 7 above to select the named range: Workbooks("BOOK2.XLS").Sheets("Sheet2").Activate To select the named range "Test" on a worksheet in a different workbook, you can use the following example: Application.Goto _ How to Select a Named Range on a Worksheet in a Different Workbook Or, you can activate the worksheet, and then use method 7 above to select the named range: Sheets("Sheet1").Activate To select the named range "Test" on another worksheet in the same workbook, you can use the following example: Application.Goto Sheets("Sheet1").Range("Test") How to Select a Named Range on Another Worksheet in the Same Workbook To select the named range "Test" on the active worksheet, you can use either of the following examples: Range("Test").Select How to Select a Named Range on the Active Worksheet Or, you can activate the worksheet, and then use method 4 above to select the range: Workbooks("BOOK2.XLS").Sheets("Sheet1").ActivateĪctiveSheet.Range(Cells(4, 5), Cells(12, 6)).Select To select the range E4:F12 on a worksheet in a different workbook, you can use either of the following examples: Application.Goto Workbooks("BOOK2.XLS").Sheets("Sheet1").Range("E4:F12") How to Select a Range of Cells on a Worksheet in a Different Workbook Or, you can activate the worksheet, and then use method 4 above to select the range: Sheets("Sheet3").ActivateĪctiveSheet.Range(Cells(3, 4), Cells(11, 5)).Select

To select the range D3:E11 on another worksheet in the same workbook, you can use either of the following examples: Application.Goto ActiveWorkbook.Sheets("Sheet3").Range("D3:E11")Īpplication.Goto ActiveWorkbook.Sheets("Sheet3").Range("D3", "E11") How to Select a Range of Cells on Another Worksheet in the Same Workbook Or, alternatively, it could be simplified to this: To select the range C2:D10 on the active worksheet, you can use any of the following examples: ActiveSheet.Range(Cells(2, 3), Cells(10, 4)).SelectĪctiveSheet.Range(ActiveSheet.Cells(2, 3), ActiveSheet.Cells(10, 4)).Select

How to Select a Range of Cells on the Active Worksheet Or, you can activate the worksheet, and then use method 1 above to select the cell: Workbooks("BOOK2.XLS").Sheets("Sheet1").Activate To select cell F7 on a worksheet in a different workbook, you can use either of the following examples: Application.Goto Workbooks("BOOK2.XLS").Sheets("Sheet1").Cells(7, 6)Īpplication.Goto Workbooks("BOOK2.XLS").Sheets("Sheet1").Range("F7") How to Select a Cell on a Worksheet in a Different Workbook Or, you can activate the worksheet, and then use method 1 above to select the cell: Sheets("Sheet2").Activate To select cell E6 on another worksheet in the same workbook, you can use either of the following examples: Application.Goto ActiveWorkbook.Sheets("Sheet2").Cells(6, 5)Īpplication.Goto (ActiveWorkbook.Sheets("Sheet2").Range("E6")) How to Select a Cell on Another Worksheet in the Same Workbook To select cell D5 on the active worksheet, you can use either of the following examples: ActiveSheet.Cells(5, 4).Select How to Select a Cell on the Active Worksheet Selection to refer to the currently selected range Rows.Count to count the number of rows in the specified item Property UseĪctiveWorkbook to specify the active workbookĬolumns.Count to count the number of columns in the specified item

The examples in this article use the properties in the following table. The examples in this article use the Visual Basic methods listed in the following table. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. Microsoft provides programming examples for illustration only, without warranty either expressed or implied.
