Rows function vba

Rows function vba DEFAULT

VBA Select, Row, Column

VBA SelectVBA Select CurrentRegionVBA ActiveCellVBA SelectionSelection FillDownVBA EntireRow and EntireColumnVBA Rows and ColumnsVBA Row and Column

VBA Select

It is very common to find the .Select methods in saved macro recorder code, next to a Range object.

.Select is used to select one or more elements of Excel (as can be done by using the mouse) allowing further manipulation of them.

Selecting cells with the mouse:

Select with the Mouse

Selecting cells with VBA:

Each of the above lines select the range from "A1" to "E9".

Select with VBA

VBA Select CurrentRegion

If a region is populated by data with no empty cells, an option for an automatic selection is the CurrentRegion property alongside the .Select method.

CurrentRegion.Select will select, starting from a Range, all the area populated with data.


Make sure there are no gaps between values, as CurrentRegion will map the region through adjoining cells (horizontal, vertical and diagonal).

With all the adjacent data

CurrentRegion Data

Not all adjacent data

CurrentRegion Missing Data

"C4" is not selected because it is not immediately adjacent to any filled cells.

VBA ActiveCell

The ActiveCell property brings up the active cell of the worksheet.

In the case of a selection, it is the only cell that stays white.

A worksheet has only one active cell.


Usually the ActiveCell property is assigned to the first cell (top left) of a Range, although it can be different when the selection is made manually by the user (without macros).

ActiveCell Under

The AtiveCell property can be used with other commands, such as Resize.

VBA Selection

After selecting the desired cells, we can use Selection to refer to it and thus make changes:

Example Selection

Selection also accepts methods and properties (which vary according to what was selected).

Clear Selection

As in this case a cell range has been selected, the Selection will behave similarly to a Range. Therefore, Selection should also accept the .Interior.Color property.

RGB (Red Green Blue) is a color system used in a number of applications and languages. The input values for each color, in the example case, ranges from 0 to 255.

Selection FillDown

If there is a need to replicate a formula to an entire selection, you can use the .FillDown method

Before the FillDown

Before Filldown

After the FillDown

After FillDown

.FillDown is a method applicable to Range. Since the Selection was done in a range of cells (equivalent to a Range), the method will be accepted.

.FillDown replicates the Range/Selection formula of the first line, regardless of which ActiveCell is selected.

.FillDown can be used at intervals greater than one column (E.g. Range("B1:C2").FillDown will replicate the formulas of B1 and C1 to B2 and C2 respectively).

VBA EntireRow and EntireColumn

You can select one or multiple rows or columns with VBA.

Select EntireColumn

The selection will always refer to the last command executed with Select.

To insert a row use the Insert method.

To delete a row use the Delete method.

VBA Rows and Columns

Just like with the EntireRow and EntireColumn property, you can use Rows and Columns to select a row or column.

Select Rows

To hide rows:

Hidden Cells

In the above example, rows 1 to 3 of the worksheet were hidden.

VBA Row and Column

Row and Column are properties that are often used to obtain the numerical address of the first row or first column of a selection or a specific cell.

The results of Row and Column are often used in loops or resizing.

Consolidating Your Learning

Suggested Exercise

Ascending Order is learning website. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. All Rights Reserved.

Excel ® is a registered trademark of the Microsoft Corporation.

© 2021 SuperExcelVBA | ABOUT

Protected by Copyscape

Thank you for contributing. A message was sent reporting your comment.


The Rows property represents all the rows on the specified worksheet or range.

We are going to use the following example.

Each cell inside the range B2 to C7 has values.

Let’s run the following code.


Delete all rows

It will leave us with a blank worksheet.

That happens because we are deleting all rows in the selected range. In this example, we would have the same effect if we used Range(“B2:C7”).Columns.Delete.

Delete the selected row

In order to delete a single row, we have to use the row number. Let’s delete the second row inside the selected range.

If we run the above code, we are going to get the following result.

The second row is removed from the selected range.

Delete a range of rows

You can specify a range inside a range. For example, let’s select the range B2, and inside this range we are going to select rows 2, 3, and 4 and delete them. We could do this using the following lines of code:

But it’s easier and more elegant to use the following code, where you delete these rows using a single line.

After you run it, Excel will remove the rows with the given numbers and you are going to get this result.

Count rows

If you run the following code, Excel will count all the rows inside the entire worksheet.

The result is 1,048,576 – the number of rows inside a worksheet.

If you want to quickly find the number of rows inside a worksheet, press Ctrl + Down Arrow. The cursor will be instantly moved to the last row.

In a similar way, you can get the number of rows inside a range.

The code returns 6.

Highlight even rows

In a lesson about the Range.Row property I showed how you can highlight even rows inside the selected range. This time we are going to achieve the same result using Range.Rows property.

As we know Rows property represents all rows inside the selected range. We have to use indexes to distinguish single rows.

Code explanation:

2. First, we assign the selected range to the myRange variable, so we don’t have to use Range(“B2:C7”) all the time.

3. The loop will go from 1 to myRange.Rows.Count, which is 6.

4. For each iteration, it will check whether there is a reminder after division by 2 (modulo). In other words, it will highlight every other row.

This is the result.

Post Views:29,080

  1. Magento 2 product alerts
  2. Highland 2 ipad
  3. Flower chart template
  4. Corey miller facebook

Excel VBA Row Count

In VBA programming referring to rows is most important as well and counting rows is one of the things you must be aware of when it comes to VBA coding. There is a lot of value we can get if we can understand the importance of counting rows which has data in the worksheet. In this article, we will show you how to count rows using VBA coding.

VBA Row Count

You are free to use this image on your website, templates etc, Please provide us with an attribution linkArticle Link to be Hyperlinked
For eg:
Source: VBA Row Count (

How to Count Rows in VBA?

You can download this VBA Row Count Excel Template here – VBA Row Count Excel Template

Example #1

To count rowsThere are numerous ways to count rows in Excel using the appropriate formula, whether they are data rows, empty rows, or rows containing numerical/text values. Depending on the circumstance, you can use the COUNTA, COUNT, COUNTBLANK, or COUNTIF more, we need to make use of RANGE object, in this object, we need to use the ROWS object, and in this, we need to use COUNT property.

For example, look at the below data in excel.

VBA Row Count Example 1

From the above data, we need to identify how many rows are there from the range A1 to A8. So first define the variable as integer to store the number of rows.


Sub Count_Rows_Example1() Dim No_Of_Rows As IntegerEnd Sub
VBA Row Count Example 1-1

For this variable, we will assign row numbers, so enter the variable name and out the equal sign.


Sub Count_Rows_Example1() Dim No_Of_Rows As Integer No_Of_Rows = End Sub
VBA Row Count Example 1-2

We need to provide a range of cells, so open RANGE object and supply the range as “A1:A8”. Code:

Sub Count_Rows_Example1() Dim No_Of_Rows As Integer No_Of_Rows = Range("A1:A8") End Sub
VBA Row Count Example 1-3

Once the range is supplied we need to count the number of rows, so choose ROWS property of RANGE object.

VBA Row Count Example 1-4

In ROWS property of RANGE object we are counting a number of rows, so choose “COUNT” property now.

VBA Row Count Example 1-7

Now in the message box show the value of the variable.


Sub Count_Rows_Example1() Dim No_Of_Rows As Integer No_Of_Rows = Range("A1:A8").Rows.Count MsgBox No_Of_Rows End Sub
VBA Row Count Example 1-5

Now run the code and see the count of rows of supplied range of cells.

VBA Row Count Example 1-6

Ok, there are 8 rows supplied for the range, so rows count is 8 in the message box.

Example #2

We have other ways of counting rows as well, for the above method, we need to supply a range of cells, and in this range cells, it shows the number of rows selected.

But imagine the scenario where we need to find the last used of any column, for example, take the same data as seen above.

VBA Row Count Example 1

To move to the last used cell from cell A1, we press the shortcut excel key“Ctrl + Down Arrow”, so it will take you to the last cell before the empty cell.

First, supply the cell as A1 using the RANGE object.


Sub Count_Rows_Example2() Dim No_Of_Rows As Integer No_Of_Rows = Range("A1") MsgBox No_Of_Rows End Sub
VBA Row Count Example 2

From this cell, we need to move down, and in the worksheet, we use Ctrl + Down Arrow, but in VBA we use END propertyEnd is a VBA statement that can be used in a variety of ways in VBA applications. Anywhere in the code, a simple End statement can be used to instantly end the execution of the code. In procedures, the end statement is used to end a subprocedure or any loop function, such as 'End if'.read more, choose this property and open bracket to see options.

Example 2-1

Look there with END key we can see all the arrow keys like “xlDown, xlToLeft, xlToRight, and xlUp” since we need to move down use “xlDown” option.


Sub Count_Rows_Example2() Dim No_Of_Rows As Integer No_Of_Rows = Range("A1").End(xlDown) MsgBox No_Of_Rows End Sub
Example 2-2

This will take you to the last cell before any break, now in the active cellThe active cell is the currently selected cell in a worksheet. Active cell in VBA can be used as a reference to move to another cell or change the properties of the same active cell or the cell's reference provided from the active more we need the row number, so use ROW property.


Sub Count_Rows_Example2() Dim No_Of_Rows As Integer No_Of_Rows = Range("A1").End(xlDown).Row MsgBox No_Of_Rows End Sub
Example 2-3

Ok, done. Now, this will show the last row number, and that will be the count of a number of rows.

VBA Row Count Example 2-4

So in rows, we have data.

Example #3 – Find Last Used Row

Finding the last used row is so important to decide how many times the loop has to run and also in the above method the last row stops to select if there is any breakpoint cell, so in this method, we can find the last used row without any problems.

Open CELL propertyCells are cells of the worksheet, and in VBA, when we refer to cells as a range property, we refer to the same cells. In VBA concepts, cells are also the same, no different from normal excel more.


Sub Count_Rows_Example3() Dim No_Of_Rows As Integer No_Of_Rows = Cells( MsgBox No_Of_Rows End Sub
VBA Row Count Example 3

Now we need to mention the row number to start with, the problem here is we are not sure how many rows of data we have, so what we can do is we straight away go to the last row of the worksheet, for this mention ROWS.COUNT property.


Sub Count_Rows_Example3() Dim No_Of_Rows As Integer No_Of_Rows = Cells(Rows.Count, MsgBox No_Of_Rows End Sub
Example 3-1

Next, we need to mention in which column we are finding the last used row, so in this case, we are finding in the first column, so mention 1.


Sub Count_Rows_Example3() Dim No_Of_Rows As Integer No_Of_Rows = Cells(Rows.Count, 1) MsgBox No_Of_Rows End Sub
Example 3-2

At this moment, it will take you to the last cell of the first column, from there onwards we need to move upwards to go the last used cell, so use End(xlUp) property.


Sub Count_Rows_Example3() Dim No_Of_Rows As Integer No_Of_Rows = Cells(Rows.Count, 1).End(xlUp) MsgBox No_Of_Rows End Sub
Example 3-3

So this will take you to the last used cell of column 1, and in this cell, we need the row number, so use ROW property to get the row number.


Sub Count_Rows_Example3() Dim No_Of_Rows As Integer No_Of_Rows = Cells(Rows.Count, 1).End(xlUp).Row MsgBox No_Of_Rows End Sub
VBA Row Count Example 3-4

Things to Remember

  • COUNT will give a number of rows in the worksheet.
  • If you have a range, then it will give a number of rows selected in the range.
  • ROW property will return the active cell row number.

Recommended Articles

This has been a guide to VBA Row Count. Here we discuss how to count used rows in excel using VBA Coding along with practical examples and downloadable excel template. You may learn more about excel from the following articles-

All in One Excel VBA Bundle (35 Courses with Projects)
  • 35+ Courses
  • 120+ Hours
  • Full Lifetime Access
  • Certificate of Completion
Excel VBA Basics #11 Create your Own Custom Functions with or without Arguments

VBA Code

No Row or Column Objects

There are no Row or Column objects and there are no Rows or Columns collections.
It is possible however to use the properties from the activesheet object


This returns a Range object that refers to a particular column.

Range("C1").ColumnWidth = Range("A1").ColumnWidth

icount = Selection.Columns.Count - returns the number of columns currently selected

The EntireColumn and EntireRow properties return the columns or rows in which the given range is located.
These are then treated as normal ranges of cells.

ActiveSheet.Outlines.ShowLevels RowLevels:=2

Range("B2:B10").EntireColumn.Interior.ColorIndex = 27

Application.Intersect(Activesheet.Rows(2).Cells, Activesheet.Columns(4).Cells)



Number of rows currently selected

lcount = Selection.Rows.Count

Number of columns in a multiple selection

Dim iareacount AsInteger
Dim itotal as Integer
   itotal = 0
   For iareacount = 1 to Selection.Areas.Count
      itotal = itotal + Selection.Areas(iareacount).Columns.Count
   Next iareacount
   Call MsgBox(itotal)


Also called GetColumnName, ColumnNumberToLetter


I would like to hide a row if certain values are entered in three cells. For e.g. if United Kingdom is selected in Cell C3 and C5 and CI is selected from cell C10, I would then have Row 16 hidden. I would like this to be dynamically i.e. updated whenever the value in the cell changes.

PrivateSub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range

    Set rng = Me.Range("C3,C5,C10")

    IfNot Intersect(rng, Target) IsNothingThen
        Rows(16).EntireRow.Hidden = Range("C3").Value = _
                    "United Kingdom" And Range("C5").Value = _
                    "United Kingdom" And Range("C10") = "C1"

© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions LimitedTopPrevNext

Vba rows function

Entire Rows and Columns

This example teaches you how to select entire rows and columns in Excel VBA. Are you ready?

Place a command button on your worksheet and add the following code lines:

1. The following code line selects the entire sheet.


Entire Sheet in Excel VBA

Note: because we placed our command button on the first worksheet, this code line selects the entire first sheet. To select cells on another worksheet, you have to activate this sheet first. For example, the following code lines select the entire second worksheet.


2. The following code line selects the second column.



3. The following code line selects the seventh row.



4. To select multiple rows, add a code line like this:


Multiple Rows

5. To select multiple columns, add a code line like this:


Multiple Columns

6. Be careful not to mix up the Rows and Columns properties with the Row and Column properties. The Rows and Columns properties return a Range object. The Row and Column properties return a single value.

Code line:

MsgBox Cells(5, 2).Row


Row Property

7. Select cell D6. The following code line selects the entire row of the active cell.



Note: border for illustration only.

8. Select cell D6. The following code line enters the value 2 into the first cell of the column that contains the active cell.

ActiveCell.EntireColumn.Cells(1).Value = 2


Note: border for illustration only.

9. Select cell D6. The following code line enters the value 3 into the first cell of the row below the row that contains the active cell.

ActiveCell.EntireRow.Offset(1, 0).Cells(1).Value = 3

EntireRow + Offset

Note: border for illustration only.

Excel VBA - Rows \u0026 Columns Insert

Range.Rows property (Excel)

Returns a Range object that represents the rows in the specified range.



expression A variable that represents a Range object.

To return a single row, use the Item property or equivalently include an index in parentheses. For example, both and return the first row of the selection.

When applied to a Range object that is a multiple selection, this property returns rows from only the first area of the range. For example, if the Range object has two areas—A1:B2 and C3:D4—, returns 2, not 4. To use this property on a range that may contain a multiple selection, test Areas.Count to determine whether the range is a multiple selection. If it is, loop over each area in the range, as shown in the third example.

The returned range might be outside the specified range. For example, returns cells A5:B5. For more information, see the Item property.

Using the Rows property without an object qualifier is equivalent to using ActiveSheet.Rows. For more information, see the Worksheet.Rows property.


This example deletes the range B5:Z5 on Sheet1 of the active workbook.

This example deletes rows in the current region on worksheet one of the active workbook where the value of cell one in the row is the same as the value of cell one in the previous row.

This example displays the number of rows in the selection on Sheet1. If more than one area is selected, the example loops through each area.

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.


Now discussing:


249 250 251 252 253