+ Reply to Thread
Results 1 to 3 of 3

Delete entire row if certains cells are blank

Hybrid View

ZH1 Delete entire row if certains... 02-11-2011, 11:46 AM
Colin Legg Re: Delete entire row if... 02-11-2011, 11:51 AM
ZH1 Re: Delete entire row if... 02-11-2011, 01:42 PM
  1. #1
    Registered User
    Join Date
    02-11-2011
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    2

    Delete entire row if certains cells are blank

    Hi all,

    Need some help in deleting entire an entire row if certain cells in that row are blank. For example:

    A1 B1 C1 D1 E1 G1
    1 1 1 1
    1 1 1 1 1 1
    1 1 1

    I want to delete only those rows that have a blank cell. Any help would be greatly appreciated. Please note that I have about 10k rows that are like the example above.

  2. #2
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Delete entire row if certains cells are blank

    Hello,

    Try this:

     
    Sub Foo()
        Dim lngLastRow As Long
        Dim rngToCheck As Range, rngToDelete As Range
     
        Application.ScreenUpdating = False
        
        With Sheet1
     
            'find the last row in the range
            lngLastRow = GetLastRow(.Range("A:G"))
     
            If lngLastRow > 1 Then
                'we want to check the used range in columns A to G
                Set rngToCheck = .Range(.Cells(1, "a"), .Cells(lngLastRow, "g"))
     
                'if there are no blank cells then there will be an error
                On Error Resume Next
                Set rngToDelete = rngToCheck.SpecialCells(xlCellTypeBlanks)
                On Error GoTo 0
     
                'allow for overlapping ranges
                If Not rngToDelete Is Nothing Then _
                        Application.Intersect(.Range("A:A"), rngToDelete.EntireRow).EntireRow.Delete
            End If
        End With
     
        Application.ScreenUpdating = True
    End Sub
     
     
    Public Function GetLastRow(ByVal rngToCheck As Range) As Long
        Dim rngLast As Range
     
        Set rngLast = rngToCheck.Find(what:="*", searchorder:=xlByRows, searchdirection:=xlPrevious)
     
        If rngLast Is Nothing Then
            GetLastRow = rngToCheck.Row
        Else
            GetLastRow = rngLast.Row
        End If
     
    End Function
    Change Sheet1 to the codename of the sheet you want to do this on.
    Last edited by Colin Legg; 02-11-2011 at 11:56 AM.
    Hope that helps,

    Colin

    RAD Excel Blog

  3. #3
    Registered User
    Join Date
    02-11-2011
    Location
    US
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Delete entire row if certains cells are blank

    Thank you so much for your help, that worked like a charm.

+ 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