UsedRange in VBA Excel

The UsedRange in VBA is a worksheet property that returns a range object representing the range used (all Excel cells used or filled in a worksheet) on a particular worksheet. It is a property representing the area covered or bounded by the top-left used cell and the last right-used cells in a worksheet.

We can describe a ‘Used Cell’ as a cell containing any formula, formatting, value, etc. We can also select the last used cell by pressing the CTRL+END keys on the keyboard.

Following is an illustration of a UsedRange in a worksheet:

We can see in the above screenshot that the UsedRange is A1:D5.

Examples of Excel VBA UsedRange Property

Let us look at some examples below to see how we can use the UsedRange property in a worksheet to find the used range in VBARange In VBARange is a property in VBA that helps specify a particular cell, a range of cells, a row, a column, or a three-dimensional range. In the context of the Excel worksheet, the VBA range object includes a single cell or multiple cells spread across various rows and columns.read more:

Example #1

We have an Excel file containing two worksheets, and we wish to find and select the used range on Sheet1.

Let us see what the Sheet1 contains:

We use the UsedRange property in the VBA immediate window to accomplish this task. VBA immediate window is a tool that helps to get information about Excel files and quickly execute or debug any VBA codeVBA CodeVBA code refers to a set of instructions written by the user in the Visual Basic Applications programming language on a Visual Basic Editor (VBE) to perform a specific task.read more, even if the user is not writing any macros. It is located in the Visual Basic Editor and can be accessed as follows:

  • Go to the Developer tab ExcelDeveloper Tab ExcelEnabling the developer tab in excel can help the user perform various functions for VBA, Macros and Add-ins like importing and exporting XML, designing forms, etc. This tab is disabled by default on excel; thus, the user needs to enable it first from the options menu.read more, click on Visual Basic Editor, or press Alt+F11 to open the Visual Basic Editor window.

In doing this, a window opens as follows:

  • Next, press Ctrl+G to open the immediate window and type the code.

The immediate window looks like this:

  • The following code will select the used range on Sheet1.

Code:

?Worksheets(“Sheet1”).Activate True ?ActiveSheet.UsedRange.Select True

The first statement of the code will activate Sheet1 of the file, and the second statement will select the used range in that active sheet.

On writing this code, we see that the range used in Sheet1 gets selected as follows:

Example #2

In this example, we wish to find the total number of rows used in Sheet1. To do this, we follow the below steps:

  • First, create a Macro name in the module.

Sub TotalRows()

End Sub

  • Define the variable TotalRow as an Integer in VBAInteger In VBAIn VBA, an integer is a data type that may be assigned to any variable and used to hold integer values. In VBA, the bracket for the maximum number of integer variables that can be kept is similar to that in other languages. Using the DIM statement, any variable can be defined as an integer variable.read more:

Sub TotalRows()

Dim TotalRow As Integer

End Sub

  • Now, assign the variable TotalRow with the formula to calculate the total number of rows:

Sub TotalRows()

Dim TotalRow As Integer

TotalRow = ActiveSheet.UsedRange.Rows.Count

End Sub

  • Now, the resultant value of TotalRow can be displayed and returned using a VBA message boxUsing A VBA Message BoxVBA MsgBox function is an output function which displays the generalized message provided by the developer. This statement has no arguments and the personalized messages in this function are written under the double quotes while for the values the variable reference is provided.read more (MsgBox) as follows:

Sub TotalRows()

Dim TotalRow As Integer

TotalRow = ActiveSheet.UsedRange.Rows.Count

MsgBox TotalRow

End Sub

  • Now, we run this code manually or by pressing F5, and we get the total number of rows used in Sheet1 displayed in a message box as follows:

So, we can see in the above screenshot that ‘5’ is returned in the message box. As shown in Sheet1, the total number of rows in the used range is 5.

Example #3

Similarly, if we wish to find the total number of columns used in Sheet1, we will follow the same steps as above except for a slight change in the code as follows:

Sub TotalCols()

Dim TotalCol As Integer

TotalCol = ActiveSheet.UsedRange.Columns.Count

MsgBox TotalCol

End Sub

Now, when we run this code manually or by pressing F5, we get the total number of columns used in Sheet1 displayed in a message box as follows:

So, ‘4’ is returned in the message box, and as we can see in Sheet1, the total number of columns in the used range is 4.

Example #4

Let’s say we wish to find the last used row and column number in Sheet2 of the file. But, first, let us see what the Sheet2 contains:

To do this, we follow the below steps:

Sub LastRow()

End Sub

  • Define the variable LastRow as Integer.

Sub LastRow()

Dim LastRow As Integer

End Sub

  • Now, assign the variable LastRow with the formula to calculate the last used row number:

Sub LastRow()

Dim LastRow As Integer

LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row

End Sub

The SpecialCells Method in Excel VBA returnsVBA Returns"GoSub" in the GoSub Return Statement signifies that it will go to the indicated line of code and complete a task until it finds the statement “Return.” It’s a lifesaver for those who know the in and out of VBA coding.read more a range object that represents only the types of cells specified. The syntax for the SpecialCells method is:

RangeObject.SpecialCells (Type, Value)

The above code, xlCellTypeLastCell: represents the last cell in the used range.

Note: ‘xlCellType’ will even include empty cells with the default format of any of their cells changed.

  • Now, the resultant value of the LastRow number can be displayed and returned using a message box (MsgBox) as follows:

Sub LastRow()

Dim LastRow As Integer

LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row

MsgBox LastRow

End Sub

  • Now, we run this code manually or by pressing F5, and we get the last used row number in Sheet2 displayed in a message box as follows:

So, we can see in the above screenshot that ‘12’ is returned in the message box. As shown in Sheet2, the last used row number is 12.

Similarly, if we wish to find the last used column number in Sheet2, we will follow the same steps as above except for a slight change in the code as follows:

Sub LastCol()

Dim LastCol As Integer

LastCol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column

MsgBox LastCol

End Sub

Now, when we run this code manually or by pressing F5, we get the last used column number in Sheet2 displayed in a message box as follows:

So, we can see in the above screenshot that ‘3’ is returned in the message box. As shown in Sheet2, the last used column number is 3.

Things to Remember About VBA UsedRange

  • VBA UsedRange is a rectangle range.VBA UsedRange includes cells having any data or being formatted etc.Excel VBA UsedRange does not necessarily include the top-left cell of the worksheet.UsedRange does not necessarily consider the active cell as used.UsedRange can be used to find the last used row in VBALast Used Row In VBAThe End(XLDown) method is the most commonly used method in VBA to find the last row, but there are other methods, such as finding the last value in VBA using the find function (XLDown).read more and to reset the used range, etc.Pressing the shortcut Excel keysShortcut Excel KeysAn Excel shortcut is a technique of performing a manual task in a quicker way.read more CTRL+SHIFT+ENTER on a keyboard can be used to extend the selection from an active cell to the last used cell on a worksheet.

This article has been a guide to VBA UsedRange. Here, we will show you how the UsedRange Property in a worksheet finds the used rows and columns in Excel VBA, practical examples, and a downloadable template. Below you can find some useful Excel VBA articles: –

  • Pressing the shortcut Excel keysShortcut Excel KeysAn Excel shortcut is a technique of performing a manual task in a quicker way.read more CTRL+SHIFT+ENTER on a keyboard can be used to extend the selection from an active cell to the last used cell on a worksheet.

  • VBA FilterExcel VBA Activate SheetVBA Val