×

Finding Last Row or Column in Excel VBA

Finding Last Row or Column in VBA

Finding the last used row, column, or cell is one very commonly used task when we write macros and VBA applications.  Like other codes in Excel and VBA, there are many methods to achieve this.

Method 1

The following method to find the Last Row will return the same result, unlike using the shortcut key CTRL + Arrow. In this method, we first select the range and then find the last row or column by using ‘End (xlDown)’ for row and ‘End (xlToRight) for column and returns the last Row or column number.

Limitation

This method will only work if the given data is contiguous if there are some blank cells, and the text is being entered after the blank cells, the cell row is being not considered.

Program: Method 1 to find the last row

'Declaring Variable
 Dim lrow As Byte    
 'Passing Value to Variable
 lrow = Range("A5").End (xlDown).Row
 'Displaying LastRow value using MsgBox function
 MsgBox "The last row number is " & lrow
 End Sub 

Output

The last row number is 16.

Method 1 to find the last row

Program 2: Method 1 to find the last column

Sub FindCol_Method1()   
 'Declaring Variable
  Dim lcol As Byte
  'Passing Value to Variable
  lcol = Range("A5").End(xlToRight).Column
 'Displaying LastColumn value using MsgBox function
 MsgBox "The last column number is " & lcol
 End Sub 

Output

The last column number is 4

Method 1 to find the last column

Program: The Limitation of Method 1

Sub FindRow_Method1()
     'Declaring Variable
     Dim lrow As Byte
     Dim lcol As Byte
     'Passing Value to Variable
     lrow = Range("A5").End(xlDown).Row
     lcol = Range("A5").End(xlToRight).Column
     'Displaying LastRow value using MsgBox function
     'the output will be 16 wherein the last row is 18 
     MsgBox "The last row number is " & lrow
     'Displaying LastColumn value using MsgBox function
     'the output will be 4 wherein last column is 6
     MsgBox "The last column number is " & lcol
 End Sub 

Output

The Limitation of Method 1

Method 2

This method is the solution of Method 1’s limitation. The steps of Method 2 are as follows:

  1. We select the Range i.e., set the columns and rows. We set the column to “A” and for rows we firstly move to the last row of the excel sheet (1048576) by “Rows.Count” feature which represents the last count of the row.  
  2. From downwards we move up to the last entered cell with the help of End(xlUp) method.
  3. At last, calculating the row number.

Program: Method 2 to find the last row

Sub FindRow_Method2()
Dim lrow As Byte
lrow = Range("A" & Rows.Count).End(xlUp).Row
'Rows.count method will return the count of total number of rows
MsgBox "The last row number is " & lrow
End Sub 

Output

The last row number is 21

Method 2 to find the last row

Method 3

Method 3 is the same, unlike Method 2 with the only difference of the range object. Here, we have used another range object i.e., cells (rows, columns). We set the row to the last row cell of the excel sheet and set the column to 1. And then, move up to and select last entered cell with the help of the End(xlUp) method.

For Column, we follow the same approach. We fix the row and position the cursor to the last column cell and select the first entered cell while moving to the left.  And calculate the column’s count for that particular column.

Program: To find the last row with the help of Method 3

Sub FindRow_Method3()
Dim lrow As Byte
lrow = Cells(Rows.Count, 1).End(xlUp).Row
MsgBox "The last row number is " & lrow
End Sub 

Output

The last row number is 21.

To find the last row with the help of Method 3

Program: Method 3 to find the last column in the Excel sheet

Sub FindCol_Method3()
Dim lcol As Byte
lcol = Cells(5, Columns.Count).End(xlToLeft).Column
MsgBox lcol
End Sub 

Output

6

Method 3 to find the last column in the Excel sheet

Method 4

Method 4 is one of simple methods to find the last row and column. In this, we have used the “SpecialCells” property to find the last row of the excel sheet. SpecialCells property is the same as GoTo Command in Excel.

Program

Sub FindRow_Method3()
'SpecialCells property is same as GoTo Command in Excel
lrow = Range("A5").SpecialCells(xlCellTypeLastCell).Row 
MsgBox lrow
End Sub 

Selection

Selection is one of the features available with VBA.  This property refers to the currently selected range item or values that can be used to manipulate the data.

The keyword “select” is used to perform the Selection operation. Generally, the operations that one performs with the Range Objects can also be performed with Selection.

Simple Selection

The simple selection, in Excel, would select only the current region of the sheet. The data should be contiguous without any break. Simple Selection is useful when we deal with continuous data. This selection method won’t select the other chunks of the data present in the sheet. This is the limitation of Simple Selection.

Program

Sub Simple_Selection()
Range("A5").CurrentRegion.Select
End Sub 

Output

Simple Selection

Dynamic Selection

Dynamic Selection is used to select a range that includes every cell that contains data present on the excel spreadsheet. It is used to create a dynamic macro, formatting of each cell, search any value, and many other important operations are performed only after dynamic selection.

Logic of Dynamic Selection

  1. In the following program, firstly we have calculated the last row and last column of the excel spreadsheet (we have explained it in the above session).
  2. With the help of the “Cells” object, we find the cell containing the last column and last row.
  3. Using “Range,” select the cells containing any data. In the first position, allocate the address of the first cell with data, and in the second place, assign the last entered the cell. At last, with the help of select property, select the in-between region.

Program

Sub DynamicSelection()
Finding the last row
lrow = Range("A" & Rows.Count).End(xlUp).Row
‘Finding the last column
lcol = Cells(5, Columns.Count).End(xlToLeft).Column   
Range("A5", Cells(lrow, lcol)).Select
End Sub 

Output


Related Topics

Excel VBA For Each Loop

A For Each loop executes a statement or a group of statements for each element in an array or collection. It repeats the statement/condition/code for each element in a collection. For Each Loops loop through every...

4 minutes read.

Excel VBA Chr Function

VBA Chr Function: The Chr function in VBA returns the character equivalent to a supplied character code between 0 and 255. Syntax Chr (CharCode) Parameter CharCode (required) – This parameter represents the character code for...

1 minute read.

VBA Charts Basic Operations

VBA Charts- Basic Operations The Chart Object in Excel VBA represents the collection of all the charts sheet present in a workbook. A chart can be either an embedded chart or a separate chart sheet. The...

6 minutes read.

Excel VBA ABS Function

VBA ABS Function: The ABS function in VBA returns the absolute value of the specified number. Syntax Abs (Number) Parameter Number (required) – This parameter represents the number that you want the absolute value of. Return This...

1 minute read.

Excel VBA Join Function

Excel VBA Join Function: The Join function in VBA is used to join an array of substrings and return them all as a single string. Syntax Join (SourceArray, [Delimiter]) Parameter SourceArray (required) – This parameter...

1 minute read.

Excel VBA: Do Until….Loop

DO UNTIL….Loop The “Do Until” Loop is same unlike DO WHILE statement just that it will keep on looping till the condition is not met. This loop is used to repeat a set...

4 minutes read.

UserForm and its Properties Excel VBA

UserForm and its Properties Userform has certain properties that can be viewed as category wise (based on appearance, behavior, font) or in an alphabetic manner. The property window is used to set or...

11 minutes read.

VBA Color Index Property

What is Color Index Property? The Excel VBA Color Index is used to change the color for the cell or range of cells or text (located under the Font section). It sets the color...

5 minutes read.

Excel VBA DateDiff Function

The DateDiff function in VBA returns a Long data value representing the number of intervals between two specified dates/times where the type of interval is supplied by the user. Syntax DateDiff (Interval, Date1, Date2, [FirstDayOfWeek], [FirstWeekOfYear]) Parameter Interval (required)...

2 minutes read.

Excel VBA WeekdayName Function

Excel VBA WeekdayName Function: The WeekdayName function in VBA returns a string containing the weekday name, for the specified integer representation of a weekday. Syntax WeekdayName (Weekday, [Abbreviate], [FirstDayOfWeek]) Parameter Weekday (required) – This parameter represents...

2 minutes read.

Finding Last Row or Column in Excel VBA

Finding Last Row or Column in VBA Finding the last used row, column, or cell is one very commonly used task when we write macros and VBA applications.  Like other codes...

5 minutes read.

Scope in Visual Basics

Definition of Scope The scope of any programming language implies the area of code where the variables will be identified, accessed, and used. Every variable has a scope associated with it. The scope of...

4 minutes read.

Excel VBA Second Function

Excel VBA Second Function: The Second function in VBA returns the second element for the specified time.  Syntax Second (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the second...

1 minute read.

Excel VBA StrReverse Function

The StrReverse function returns a String after reversing the given String. Syntax StrReverse (Expression) Parameter Expression (required) – This parameter represents the String that you want to reverse. Return This function returns a String after reversing the given String. Example...

1 minute read.

Excel VBA Tan Function

VBA Tan Function: The Tan function in VBA returns the tangent for the specified angle in radians. Syntax Tan (Number) Parameter Number (required) – This parameter represents the angle supplied in radiant that you want...

1 minute read.

VBA Updating Pivot Table

VBA- Updating Pivot Table The pivot table is an important feature to explore, summarize, and interpret the bulk amount of data. It helps data evaluating, reviewing, as well as making useful...

3 minutes read.

VBA ActiveSheet

What is VBA Activesheet Property? The active sheet means the current worksheet which you are working on and viewing. The ActiveSheet object signifies the worksheet tab that is selected before running the VBA...

5 minutes read.

VBA Global Variable

What is Global Variable? The Global Variables in VBA refers to the variables declared before the start of any macro. They are defined outside the functions and are used by all the functions or...

6 minutes read.

Excel VBA DateSerial Function

The DateSerial function in VBA returns a Date from a supplied year, month, and day number. Syntax DateSerial (Year, Month, Day) Parameter Year (required) – This parameter represents an integer signifying the year. Month (required) – This parameter...

2 minutes read.

Excel VBA Error Function

VBA Error Function: The Error function in VBA returns the error message corresponding to a supplied error code. Syntax Error ([ErrorNumber]) Parameter ErrorNumber (optional) – This parameter represents the required error number. By default, the...

1 minute read.