This is an often question that turns up – how to convert a column number to a string e.g.
MyRange.Columns(3).Hidden = True 'Hides columns EĪside from the Rows and Columns properties Ranges also facilitate a Row and Column property which provide you with the number of the Ranges first row and column. MyRange.Rows(2).Hidden = True 'Hides rows 11 Similarly as with the Worksheet Range property, any Range facilitates the Rows and Columns property.
Usually however, you will want to obtain rows or columns of an existing Range. The above approach assumed you want to obtain only rows/columns from the ActiveSheet – the visible and top-most Worksheet. Range(Columns(1), Columns(3)).Hidden = True 'Hides columns A:C Range(Rows(1), Rows(3)).Hidden = True 'Hides rows 1:3 To get a range of rows/columns you need to use the Range function like so: Rows(1).Hidden = True 'Hides the first row in the ActiveSheetĬolumns(1).Hidden = True 'Hides the first column in the ActiveSheet Although, the function’s parameters suggest taking both a RowIndex and ColumnIndex actually the first argument you provide will be the column index. Similarly you can use the Columns function to obtain any single column within a Range. Although, the function’s parameters suggest taking both a RowIndex and ColumnIndex it is enough just to provide the row number. If you want to get a certain row within a Range simply use the Rows property of the Worksheet. The three properties EntireRow/EntireColumn, Rows/Columns and Row/Column are often misunderstood so read through to understand the differences. Range("B2").EntireColumns(1).Hidden = True 'Gets and hides the entire column 2 Range("B2").EntireRows(1).Hidden = True 'Gets and hides the entire row 2 Although, the function’s parameters suggest taking both a RowIndex and ColumnIndex it is enough just to provide the column number. To get and entire column of a specified Range you need to use the EntireColumn property. To get and entire row of a specified Range you need to use the EntireRow property. There are a couple of ways to obtain Worksheet rows in VBA: Getting an entire row or column The Excel VBA Range object allows you to select single or multiple rows as well as single or multiple columns. The Cells property is in fact a Range object not a separate data type.Įxcel facilitates a Cells function that allows you to obtain a cell from within the ActiveSheet, current top-most worksheet.Ĭells are Ranges which means they are not a separate data type:Īs we all know an Excel Worksheet is divided into Rows and Columns.
'Let us assume A1 contains the formula "=10+20"ĭebug.Print Range("A1").Value 'Returns: 30ĭebug.Print Range("A1").Formula 'Returns: =10+20Ī Worksheet Cells property is similar to the Range property but allows you to obtain only a SINGLE CELL, based on its row and column index. For example a cell with a formula of =10+20 will have the same Formula property. For example a cell with the formula =10+20 has an evaluated value of 20.Ī Range Formula is the formula provided in the cell or range of cells. The Range object contains a variety of properties with the main one being it’s Value and an the second one being its Formula.Ī Range Value is the evaluated property of a cell or a range of cells. This may reference a Worksheet from within a Workbook external to the Workbook in which the macro is executed as Active references simply the currently top-most worksheet. The ActiveWorkbook is not same to ThisWorkbook. Sheets("Sheet1").Range("A1").Select 'Will always select items from Worksheet named Sheet1 You might want to define the Worksheet reference by Range if you want your reference values from a specifc Worksheet: So beware as depending on your ActiveWorksheet the Range object will return values local to your worksheet: The Range object defaults to your ActiveWorksheet. Set myRange = Range("A1") 'Need to use Set to define myRange As the VBA Range is an object you need to use the Set statement: The Range is a separate object variable and can be declared as other variables. Select a cell or Range of cells using the Select method. Range("A1:B2").Activate 'Activate the cells and show them on your screen (will switch to Worksheet and/or scroll to this range. Range("A1:B2").Select 'Select the Cells A1:B2 in your Excel Worksheet Set r= Range(Range("A1"), Range ("B1")) 'Range of 2 cells A1 and B1 Set r = Range("A1:B2") 'Square Range of 4 cells - A1,A2,B1,B2 The Range is a Worksheet property which allows you to select any subset of cells, rows, columns etc.