How can I get the number of rows with text from an Excel document (and  remove blank rows)

Posted on 2005-03-08
Medium Priority
Last Modified: 2010-05-02
I have 3 Excel documents and I have to get the number of rows in each one, the number or rows that are NOT blank, but the problem is there are a lot of blank rows in between the rows with text, and right now my code counts these rows as if they weren't blank.  Plus the spreadsheet doesn't look good like this...with all these blank rows all over the place...

This is my code but it doesn't work.  The first one the CoCount variable returns the correct value, but the other two do NOT.
This is because the first one is getting the value of the original Excel spreadsheet (oSheet) where there are no blank rows in between...

The other two show the rows containing errors (oSheet_Error) or duplicates ( oSheet_Dup) from the original.  
But if the computer finds an error-row at line 12 in the original, it places it at line 12 in the error file leaving the first 11 rows blank...I need to find a way to get rid of these blank rows...Maybe after that my code will work...?!?

Dim CoCount As Integer
Dim CoErrorCount As Integer
Dim CoDupCount As Integer

CoCount = oSheet.UsedRange.Rows.count - 1
CoErrorCount = oSheet_Error.UsedRange.Rows.count - 1
CoDupCount = oSheet_Dup.UsedRange.Rows.count - 1

MsgBox (CoCount)
Question by:Angie532
  • 2
LVL 10

Accepted Solution

fds_fatboy earned 2000 total points
ID: 13487313
If you sort the rows, your blank rows will be removed.
LVL 12

Expert Comment

ID: 13487625
Here is the code you need , I believe

Sub test()
    MsgBox GetCountOfRowsWithData(ActiveWorkbook.Sheets(1))
End Sub

Function GetCountOfRowsWithData(oSheet As Worksheet) As Integer
    Dim iCount As Integer
    Dim iRows As Integer
    Dim MyRange As Range
    Set MyRange = oSheet.UsedRange
    For iRows = 1 To oSheet.UsedRange.Rows.Count
        If IsNull(MyRange.Rows(iRows).EntireRow.Text) Then
            iCount = iCount + 1
            'Blank row!!!!!
        End If
    Next iRows

    GetCountOfRowsWithData = iCount
End Function
LVL 12

Expert Comment

ID: 13487669
If you want to remove the blank rows then do this at the point I marked 'Blank row!!!!!  using


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

621 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