Consulting

Results 1 to 10 of 10

Thread: Deleting Blank Rows

  1. #1

    Deleting Blank Rows

    Hi,

    I have about 3000 rows of excel data and some of these rows are blank, which I need to remove.

    I could do a loop to check Cell A1 and if blank delete the entire row and then go onto the next row and so on, but I don't know how to end the loop as the cells at the end of the data will be blank.

    How do you get it to end at the end of the data?

    Also - is this the right way to go about it?

    Cheers,

    B.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Public Sub ProcessData()
    Dim Lastrow As Long
    Dim i As Long
    Dim cell As Range

    Application.ScreenUpdating = False

    With ActiveSheet

    Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = 2 To Lastrow 'Lastrow to 1 Step -1

    If Cells(i, "A").Value2 = "" Then

    .Rows(i).Delete
    End If
    Next i
    End With

    Application.ScreenUpdating = True
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Jul 2010
    Posts
    66
    Location
    I'd use the 'Lastrow to 1 step -1'... since you are deleteting rows, that will not throw out the count.

    GComyn

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Oops, my bad. Forgot to delete the first part.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    If Cells(i, "A").Value2 = "" Then
    It would seem that if there is data in (i,"B"), this would incorrectly delete the row

    How about (not tested since I don't have Excel on this comp)

    [VBA]
    Public Sub ProcessData()
    Dim Lastrow As Long
    Dim i As Long
    Dim cell As Range

    Application.ScreenUpdating = False

    With ActiveSheet

    For i = .UsedRange.rows.count to 1 Step -1

    If application.worksheetfunction.counta(.rows(i)) = 0 then
    .Rows(i).Delete
    End If
    Next i
    End With

    Application.ScreenUpdating = True
    End Sub
    [/VBA]


    Paul

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Paul_Hossler
    How about (not tested since I don't have Excel on this comp)
    What is the point of a computer without Excel on it?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Quote Originally Posted by xld
    What is the point of a computer without Excel on it?
    ^^Gaming. Paperweight. A way for the boss to receive e-mail.

    For the OP question, if you are concerened about truly blank cells (as opposed to cells holding a formula that evaluates to "")

    [VBA]On Error Resume Next
    Range("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    On Error Goto 0[/VBA]

  8. #8
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,064
    Location
    Quote Originally Posted by xld
    What is the point of a computer without Excel on it?
    Does Bill know about this?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  9. #9
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Similar to what Paul posted:
    [VBA]Sub DeleteBlankRows()
    Dim Rw As Range
    Dim lr As Long

    Dim start As Double
    start = Timer

    If WorksheetFunction.CountA(ActiveSheet.UsedRange) = 0 Then
    MsgBox "No data found", vbOKOnly
    Exit Sub
    End If
    With Application
    .Calculation = xlCalculationManual
    .ScreenUpdating = False

    For lr = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
    If WorksheetFunction.CountA(Range("A" & lr).EntireRow) = 0 Then
    Range("A" & lr).EntireRow.Delete xlUp
    End If
    Next lr

    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With

    MsgBox "Macro took " & (Timer - start) & " seconds to run."
    End Sub[/VBA]

  10. #10
    VBAX Newbie badri.toppur's Avatar
    Joined
    Nov 2009
    Location
    Udupi, India
    Posts
    5
    Location
    This function ProcessData(), worked just fine on my dataset, to remove umpteen blank rows corresponding to page breaks in a pdf file.

    Paul Hossler, Thank you once again.

    Quote Originally Posted by Paul_Hossler View Post
    It would seem that if there is data in (i,"B"), this would incorrectly delete the row

    How about (not tested since I don't have Excel on this comp)

    [VBA]
    Public Sub ProcessData()
    Dim Lastrow As Long
    Dim i As Long
    Dim cell As Range

    Application.ScreenUpdating = False

    With ActiveSheet

    For i = .UsedRange.rows.count to 1 Step -1

    If application.worksheetfunction.counta(.rows(i)) = 0 then
    .Rows(i).Delete
    End If
    Next i
    End With

    Application.ScreenUpdating = True
    End Sub
    [/VBA]


    Paul

Posting Permissions

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