Consulting

Results 1 to 11 of 11

Thread: Deleting Blank Rows

  1. #1
    VBAX Regular
    Joined
    Feb 2008
    Posts
    58
    Location

    Deleting Blank Rows

    I'm trying to use this code from the knowledge base:

    [VBA]Sub DeleteBlankRows()
    Dim Rw As Long, RwCnt As Long, Rng As Range

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    On Error Goto Exits:

    If Selection.Rows.Count > 1 Then
    Set Rng = Selection
    Else
    Set Rng = Range(Rows(1), Rows(ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row()))
    End If
    RwCnt = 0
    For Rw = Rng.Rows.Count To 1 Step -1
    If Application.WorksheetFunction.CountA(Rng.Rows(Rw).EntireRow) = 0 Then
    Rng.Rows(Rw).EntireRow.Delete
    RwCnt = RwCnt + 1
    End If
    Next Rw

    Exits:
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic

    End Sub
    [/VBA]

    However, it doesn't seem to be working. The database that I'm using has slightly over 300K lines, of which every other line is blank. I don't know if there is something wrong with my computer as Excel goes "non responding" and I have to use the task manager to shut it down. When I reopen the spreadsheet, no more lines have been deleted that previously, which is why it doesn't seem to be working.

    If anyone has some suggestions, I certianly would welcome anything!

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi Allison,
    I just tried your code on some sample data and it worked fine. What version of excel are you using?

    Would it be possible to post the workbook if it doesn't have any personal, private information.....maybe dummy it up.

    hit post reply and after posting your message scroll down till you find a button that says "manage attachments"
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    Feb 2008
    Posts
    58
    Location
    I put in a watch and debugged the code -- I can see that it is working now. However, it's much slower than I anticiapted. In the last 45 minutes that I was away from my desk, RwCnt increased only by 22,000.

    I'm using 2007 and will attach a workbook.

    Thanks!

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Allison, I don't have 2007 so it would be better to post a 2003 version of the file.

    have you tried something using specialcells looking specifically for blank cells such as the following?

    [vba]Sub test222()
    'Sheets("SomeSheet").UsedRange.SpecialCells(xlBlanks).EntireRow.Delete
    ActiveSheet.UsedRange.SpecialCells(xlBlanks).EntireRow.Delete
    End Sub[/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    VBAX Regular
    Joined
    Feb 2008
    Posts
    58
    Location
    I did try the specialcells suggestion, but I didn't think that was working either. It probably was working also, but since it bogged down my computer, I didn't think that it was.

  6. #6
    VBAX Regular
    Joined
    Feb 2008
    Posts
    58
    Location
    thanks for the help!

  7. #7
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Did you get a workable solution? If so could you mark your thread solved using the thread tools at the top of the page.

    I thought you were going to post a 2003 workbook...
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Here's another idea for you to try:
    [VBA]Option Explicit
    Sub DeleteEmptyRows()
    Dim lastrow As Long
    Dim r As Long
    lastrow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
    Application.ScreenUpdating = False
    For r = lastrow To 1 Step -1
    If Application.WorksheetFunction.CountA(Rows(r)) = 0 _
    Then Rows(r).Delete
    Next r
    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    VBAX Regular
    Joined
    Feb 2008
    Posts
    58
    Location

    workbook

    Here's the 2003 workbook.

  10. #10
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I don't have excel here, so cant give you a code solution. With that number of rows, I would look at Sort Descending on a suitable column and then run the code on the remaining rows where the selected column is blank, finally resorting the data to its original order. All this is subject to suitable key, of course.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This will delete rows in chunks.
    [VBA]Sub test()
    Dim keyRange As Range

    Application.ScreenUpdating = False
    Application.Calculation = xlManual

    Set keyRange = ActiveSheet.UsedRange.Rows(1).Resize(2000)

    Do
    With keyRange
    Set keyRange = .Resize(.Rows.Count * 2)
    End With

    On Error Resume Next
    keyRange.Columns(1).SpecialCells(xlCellTypeBlanks).EntireRow.Delete shift:=xlUp
    On Error GoTo 0
    Loop Until Application.Intersect(keyRange, ActiveSheet.UsedRange).Address = ActiveSheet.UsedRange.Address

    Application.Calculation = xlAutomatic
    Application.ScreenUpdating = True
    End Sub[/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •