Consulting

Results 1 to 9 of 9

Thread: Solved: Deleting Empty Rows

  1. #1
    VBAX Tutor
    Joined
    Mar 2009
    Posts
    227
    Location

    Solved: Deleting Empty Rows

    Hi guys,


    Any idea how to delete empty rows on a sheet? Random rows will be completely empty, and I need to get rid of these rows so it doesnt have any blank rows

    thanks

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]
    Sub Macro4()
    Dim Rw As Long, i As Long
    With ActiveSheet.UsedRange
    'Find last row, delete blank rows
    Rw = .Cells.Find(What:="*", after:=.Cells(1, 1), LookIn:= _
    xlFormulas, lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
    xlPrevious, MatchCase:=False).Row
    For i = Rw To 1 Step -1
    If Application.CountA(.Rows(i)) = 0 Then
    .Rows(i).EntireRow.Delete
    End If
    Next
    End With
    End Sub
    [/VBA]
    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'

  3. #3
    VBAX Contributor
    Joined
    Jul 2004
    Location
    Gurgaon, India
    Posts
    148
    Location
    Hi,

    Without loop

    [vba]Sub kTest()
    Dim r As Long, c As Long
    Application.ScreenUpdating = 0
    With ActiveSheet.UsedRange
    r = .Rows.Count
    c = .Columns.Count
    End With
    Columns(1).Insert
    With Range("a1:a" & r)
    .FormulaR1C1 = "=counta(rc[1]:rc[" & c & "])"
    .Value = .Value
    On Error Resume Next
    .Replace What:="0", Replacement:="", LookAt:=xlWhole
    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    On Error GoTo 0
    End With
    Columns(1).Delete
    Application.ScreenUpdating = 1
    End Sub[/vba]
    HTH

  4. #4
    VBAX Tutor
    Joined
    Mar 2009
    Posts
    227
    Location
    Thanks guys, it works well

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Krishna,
    Nice idea, but you need to check for the rightmost column. Try a test with data only in Column D
    Regards
    MD
    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'

  6. #6
    VBAX Regular
    Joined
    Mar 2009
    Posts
    18
    Location
    Now the question I have is can you modify this script slightly so that it will delete the entire row based off a specific column being empty. I have rows that need deleted if a specific cell in that row is empty.

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Now the question I have is can you modify this script slightly so that it will delete the entire row based off a specific column being empty. I have rows that need deleted if a specific cell in that row is empty.
    [vba]Range("J:J").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    [/vba]
    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'

  8. #8
    VBAX Contributor
    Joined
    Jul 2004
    Location
    Gurgaon, India
    Posts
    148
    Location
    Quote Originally Posted by mdmackillop
    Hi Krishna,
    Nice idea, but you need to check for the rightmost column. Try a test with data only in Column D
    Regards
    MD
    Replace

    [vba]c = .Columns.Count[/vba]

    with

    [vba]c = Columns.Count[/vba]

    But I think my code should have been like this

    [vba]Sub kTest()
    Dim r As Long, c As Long
    Application.ScreenUpdating = 0
    With ActiveSheet.UsedRange
    r = .Cells.Find(What:="*", after:=.Cells(1, 1), SearchDirection:=xlPrevious).Row
    c = .Cells.Find(What:="*", after:=.Cells(1, 1), SearchDirection:=xlPrevious).Column
    End With
    Columns(1).Insert
    With Range("a1:a" & r)
    .FormulaR1C1 = "=counta(rc[1]:rc[" & c & "])"
    .Value = .Value
    On Error Resume Next
    .Replace What:="0", Replacement:="", LookAt:=xlWhole
    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    On Error GoTo 0
    End With
    Columns(1).Delete
    Application.ScreenUpdating = 1
    End Sub[/vba]

    Kris

  9. #9
    VBAX Regular
    Joined
    Mar 2009
    Posts
    18
    Location
    As usual, mdmackillop, you've been AWESOME! I don't know about the original poster, but this code works well for me and should probably be marked solved at this point.

Posting Permissions

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