+ Reply to Thread
Results 1 to 7 of 7

deleting empty rows

Hybrid View

  1. #1
    Registered User
    Join Date
    09-14-2009
    Location
    Prague
    MS-Off Ver
    Excel 2007
    Posts
    16

    deleting empty rows

    Hi,

    I need to write a macro which checks cells in one column and if the cell is empty it deletes the whole row (which contains the cell).

    I tried this code but it doesn't delete all rows with empty cells:

    Public Sub deleterows()
    For o = 7 To 25
         If Worksheets("sheet").Cells(o, 4) = "" Then
            Rows(o).Delete
            End If
         End If
    Next o
    End Sub
    So I modified the code:

    Public Sub deleterows()
    For o = 7 To 25
         If Worksheets("sheet").Cells(o, 4) = "" Then
            Rows(o).Delete
             o = o - 1
             a = a + 1
             If a = 20 Then
                 o = 25
             End If
         End If
    Next o
    End Sub
    It works correct but it is not a proper solution (it ends the for loop after 20 deleted cells)...

    Do you have any better suggestions?

    Thanks!
    Last edited by kaipan; 09-27-2009 at 04:29 PM.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: deleting empty rows

    You need to iterate from bottom to top when you're deleting rows. Think about it ...
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: deleting empty rows

    hi Kaipan,

    Welcome to the Forum
    ... have you tried searching for a solution before posting your question?

    The macro that you have shown is designed to only check the rows between row 7 & row 25 as stated by the initial For statement & because of this, it appears it was written for a very specific purpose. Your modified attempt does recognise that once a row is deleted, the counter becomes inaccurate if the macro runs from the top to the bottom of the data, because of this it is wise to process from the bottom up (eg http://j-walk.com/ss/excel/tips/tip56.htm).

    If the above link isn't the best approach for you, "Delete row macros" are commonly asked about & I think you'll find a solution with a quick glance at some of the links on the below search:
    http://www.google.co.uk/#hl=en&sourc...14e9eaa343dff4


    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  4. #4
    Valued Forum Contributor Shijesh Kumar's Avatar
    Join Date
    05-26-2008
    Location
    Bangalore / India
    MS-Off Ver
    2000
    Posts
    717

    Re: deleting empty rows

    Sub x()
        For r = 75 To 7 Step -1
            If Cells(r, 7) = "" Then
                Cells(r, 7).EntireRow.Select
                Selection.Delete
                
            End If
        Next r
    End Sub

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: deleting empty rows

    ...or if the cells are true blanks (not formula nulls) then (avoiding iteration):

    Sub x
    On Error Resume Next
    Range(Cells(7,4),Cells(25,4)).SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End Sub

  6. #6
    Registered User
    Join Date
    09-14-2009
    Location
    Prague
    MS-Off Ver
    Excel 2007
    Posts
    16

    Re: deleting empty rows

    thank you for answers!

    next time I will first search for a solution on forum...

  7. #7
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: deleting empty rows

    Thanks for the feedback & marking the thread as solved - I'm pleased we could help :-)

    Rob

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1