VBA Used Range – Count the Number of Used Rows or Columns

Associated Files Download Links

UsedRange – Find Last Used Cell, Column or Row

The following code will return in a message box the total number of rows used in a worksheet. Empty rows are considered used if data follows the empty row.

Do you have to run a loop down a sheet but don’t know where the data stops? ActiveSheet.UsedRange.Rows.Count might help.

Put this in a module

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

alt text

Learn More!

Find First Empty Cell

Using VBA you may need to write to the first empty cell, or after the last row used in a column. There is no need to loop to find this, the following code does it for you.

In this example the code will write “FirstEmpty” in the first empty cell in column “d”

Count Used Columns In Worksheet

The following code will return in a message box the total number of columns used in a worksheet. Empty columns are considered used if data follows the empty column.

Last Used Cell – Problems

When I need to For..Next..Loop through an entire column I usually use ActiveSheet.UsedRange.Rows.Count to find where to stop. I’ve always had good luck with this approach.

I am also aware that occasionally Excel thinks the last row exists somewhere, but the row is actually empty. I’ve seen this a few times after importing data. From BeyondTechnology:

The Worksheet object’s UsedRange does not always work because the used range (or “dirty area”) of a spreadsheet may be larger than the area actually populated with your records.

I’m bookmarking the article Identifying the Real Last Cell as an alternative to UsedRange for looping through Columns, Rows, and Ranges.

<<Return to VBA Examples

Did you find this VBA tutorial useful? Then share it with your friends and colleagues:

You may also like some of this related content...

The following Subroutine will delete each row in a range where the value in Column
Find Max Value For Each Column in Range The following function will return the Maximum
Select Non-Contiguous Columns To select non contiguous columns using VBA, you need to hard code
The standard Excel “Column” Function returns the number rather than the letter of the column
If you need to get the active cell's column or row from VBA, use this