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
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
  • 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

762 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