Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the number of filled cells in a column (VBA)

Tags:

excel

vba

My first column is an index, with monotonously increasing numbers, which ends at a non predictable point. Now I want to find out, how many entries this column has. Is there a better way than iterating throug this and watch out for an empty cell?

like image 266
poeschlorn Avatar asked Jul 14 '10 09:07

poeschlorn


People also ask

How do I count the number of filled rows in VBA?

To count rows. Depending on the circumstance, you can use the COUNTA, COUNT, COUNTBLANK, or COUNTIF functions. read 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.

How do I count the number of cells in VBA?

In VBA Count function is used to count that how many number of cells have values in it, we need to remember that the cells which have numbers or text which are enclosed in double quotes or those cells whose values are typed directly are counted, those cells which have the random data which cannot be translated by excel ...


5 Answers

If you want to find the last populated cell in a particular column, the best method is:

Range("A" & Rows.Count).End(xlUp).Row

This code uses the very last cell in the entire column (65536 for Excel 2003, 1048576 in later versions), and then find the first populated cell above it. This has the ability to ignore "breaks" in your data and find the true last row.

like image 189
MrKowz Avatar answered Nov 12 '22 11:11

MrKowz


One way is to: (Assumes index column begins at A1)

MsgBox Range("A1").End(xlDown).Row

Which is looking for the 1st unoccupied cell downwards from A1 and showing you its ordinal row number.

You can select the next empty cell with:

Range("A1").End(xlDown).Offset(1, 0).Select

If you need the end of a dataset (including blanks), try: Range("A:A").SpecialCells(xlLastCell).Row

like image 25
Alex K. Avatar answered Nov 12 '22 11:11

Alex K.


You can also use

Cells.CurrentRegion

to give you a range representing the bounds of your data on the current active sheet

Msdn says on the topic

Returns a Range object that represents the current region. The current region is a range bounded by any combination of blank rows and blank columns. Read-only.

Then you can determine the column count via

Cells.CurrentRegion.Columns.Count

and the row count via

Cells.CurrentRegion.Rows.Count
like image 20
almog.ori Avatar answered Nov 12 '22 11:11

almog.ori


You may also use:

UsedRange.Rows.Count
like image 33
AMIB Avatar answered Nov 12 '22 13:11

AMIB


To find the last filled column use the following :

lastColumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
like image 37
deva ruban Avatar answered Nov 12 '22 11:11

deva ruban