Solved

vba cell range loop counter

Posted on 2014-04-03
3
1,273 Views
Last Modified: 2014-04-04
Hi all,

I am trying to develop a piece of code that will count the number of non-blank cells from a specified range.

Here is what I have thus far:

Public Function CountDataRows() As Long
 'Counts the number of non-blank cells from a START cell range. Loops through the range until empty cell is found. Returns the total number of rows that contain data within the 'specified range/column. Note: Is dependant on each row having a value.

Dim wks As worksheet
Dim wkb As Workbook
Dim rng As Range
Dim Counter As Long

Set wkb = ActiveWorkbook
Set wks = wkb.Sheets("Test")
Set rng = wks.Range("testrow")

    With wks
        Counter = 0
    
        Do Until wks.Range("testrow").Offset(Counter, 0) Is Not Empty
    
        Counter = Counter + 1
    
        Loop
    End With

CountDataRows = Counter

End Function 

Open in new window


Does anyone have any ideas on how to improve upon this?

TA
0
Comment
Question by:discogs
3 Comments
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 39977273
You probably need something like this

Public Function CountDataRows() As Long
 'Counts the number of non-blank cells from a START cell range. Loops through the range until empty cell is found. Returns the total number of rows that contain data within the 'specified range/column. Note: Is dependant on each row having a value.

Dim wks As worksheet
Dim wkb As Workbook
Dim rng As Range
Dim cel As Range
Dim Counter As Long

Set wkb = ActiveWorkbook
Set wks = wkb.Sheets("Test")
Set rng = wks.Range("testrow")

        Counter = 0
    
        for each cel in rng
        if isempty(cel) then
        Counter = Counter + 1
        end if
        next cel

CountDataRows = Counter

End Function 
                                  

Open in new window

0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39977321
HI,

You could you use this method (but it only works within the .UsedRange area)

Set myRange = Range("b1:b2")
On Error Resume Next
BlankCells = myRange.SpecialCells(xlCellTypeBlanks).Count
On Error GoTo 0
NonBlankCells = myRange.Cells.Count - BlankCells

Open in new window


If you do not know if your range is within the .UsedRange area

pls try

Set myRange = Range("a1:b3")

Set IntersectRange = Intersect(ActiveSheet.UsedRange, myRange)
On Error Resume Next
IntersectCellsCount = IntersectRange.Cells.Count
On Error GoTo 0
CellsOutsideUsedRange = myRange.Cells.Count - IntersectCellsCount
On Error Resume Next
UsedRangeEmptyCellsCount = IntersectRange.SpecialCells(xlCellTypeBlanks).Count
On Error GoTo 0
BlankCells = UsedRangeEmptyCellsCount + CellsOutsideUsedRange

NonBlankCells = myRange.Cells.Count - BlankCells

Open in new window

EDIT

to find the last empty cell before something else pls

Set myRange = Range("a1").End(xlDown).Offset(-1, 0)
'Or
CellsCount = Range(Range("A1"), Range("a1").End(xlDown).Offset(-1, 0)).Cells.Count

Open in new window

Regards
0
 

Author Closing Comment

by:discogs
ID: 39979579
Perfect. Thanks Syed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to read all the sheets in excel 12 44
Search for a value in Column? 5 22
Excel for Mac - How make those Tabs larger? 2 31
Automating Excel Weekly Report 13 64
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now