?
Solved

vba cell range loop counter

Posted on 2014-04-03
3
Medium Priority
?
1,476 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 2000 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 52

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 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