0% found this document useful (0 votes)
528 views

VBA Used Range - Count The Number of Used Rows or Columns - VBA Code For Row Count Examples

The document discusses using VBA code to count the number of used rows or columns in a worksheet. It provides code examples to return the total number of rows or columns used in the active sheet. It also provides code to find the first empty cell in a column.

Uploaded by

Sarkari Chakri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
528 views

VBA Used Range - Count The Number of Used Rows or Columns - VBA Code For Row Count Examples

The document discusses using VBA code to count the number of used rows or columns in a worksheet. It provides code examples to return the total number of rows or columns used in the active sheet. It also provides code to find the first empty cell in a column.

Uploaded by

Sarkari Chakri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

 Return to VBA Code Examples Menu (/vba-code-

examples/)

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.

MsgBox ActiveSheet.UsedRange.Rows.Count

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


:

Sub LastRow()

Dim LastRow As Integer

LastRow = ActiveSheet.UsedRange.Rows.Count

MsgBox LastRow

End Sub
Sub LastCol()

Dim LastCol As Integer

ColRow = ActiveSheet.UsedRange.Col.Count

Find First Empty Cell


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

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

Public Sub AfterLast()

ActiveSheet.Range("d" & ActiveSheet.Rows.Count).End(xlUp).Offset(1,


End Sub

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.

MsgBox ActiveSheet.UsedRange.Columns.Count

Last Used Cell – Problems


When I need to For..Next..Loop through an entire column I usually
use ActiveSheet.UsedRange.Rows.Count to �nd 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


(http://www.beyondtechnology.com/geeks012.shtml) 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!

(/out/easyvba)

Learn More! (/out/easyvba)

<<Return to VBA Examples (/vba-code-examples/)


Did you �nd this VBA tutorial useful? Then share it with your
friends and colleagues:[/fusion_builder_column]
[/fusion_builder_row][/fusion_builder_container]

(/vba-code-generator/)

(/vba-code-

generator/)
ABOUT AUTOMATE EXCEL

"At Automate Excel we o�er a range of free resources, software,


training, and consulting to help you Excel at Excel. Contact me
(https://www.automateexcel.com/excel/contact/) to learn more."

Steve Rynearson, Chief Excel O�cer (CEO) at Automate Excel

VBA CODE GENERATOR – AUTOMACRO


(HTTPS://WWW.AUTOMATEEXCEL.COM
/VBA-CODE-GENERATOR/)

VBA TUTORIAL (/LEARN-VBA-TUTORIAL)

VBA CODE EXAMPLES


(HTTPS://WWW.AUTOMATEEXCEL.COM
/VBA-CODE-EXAMPLES/)

EXCEL BOOT CAMP


(HTTPS://WWW.AUTOMATEEXCEL.COM
/EXCEL-BOOT-CAMP/)

FORMULAS TUTORIAL (/FORMULAS-


FUNCTIONS)

FORMULAS LIST
(HTTPS://WWW.AUTOMATEEXCEL.COM
/FORMULAS/)

FUNCTIONS LIST
(HTTPS://WWW.AUTOMATEEXCEL.COM
/FUNCTIONS/)

SHORTCUT COACH
(HTTPS://WWW.AUTOMATEEXCEL.COM
/SHORTCUTCOACH/)

SHORTCUT TUTORIAL (/LEARN-KEYBOARD-


SHORTCUTS)

SHORTCUTS LIST (/SHORTCUTS/)

NEWSLETTER SIGNUP

I'm constantly posting new free stu�! Sign up here to receive


notices.

First Name

Email

SUBMIT

©  2018 Spreadsheet Boot Camp LLC. All


Rights Reserved.

You might also like