VBA Used Range – Count the Number of Used Rows or Columns
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
Dim LastRow As Integer
LastRow = ActiveSheet.UsedRange.Rows.Count
Dim LastCol As Integer
ColRow = ActiveSheet.UsedRange.Col.Count
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”
Public Sub AfterLast()
ActiveSheet.Range("d" & ActiveSheet.Rows.Count).End(xlUp).Offset(1, 0).Value = "FirstEmpty"
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.
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!
Did you find this VBA tutorial useful? Then share it with your friends and colleagues: