+ Reply to Thread
Results 1 to 5 of 5

If column dont contain 0 then delete row

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-01-2012
    Location
    ZA
    MS-Off Ver
    Excel 2013
    Posts
    300

    If column dont contain 0 then delete row

    Goodday hope someone can help

    I have an array in Column H with 1300 rows, I need a Macro or a formula to do the following -If no 0 in array then delete all row that dont contain the 0. Sample below.Any help greatly appreciated. It must go through the whole column and delete. I have a macro but it does not delete all the cells.Thanx for any help

    1, 17, 0, 40, 17 Qualifies it have a 0
    1, 17, 8, 9, 40 >>>>>>>>>>>Must be deleted
    1, 17, 14, 3, 40 >>>>>>>>>>>Must be deleted
    1, 17, 17, 0, 40 >>>>>>>>>>>Qualifies
    1, 17, 17, 40, 0
    1, 17, 17, 40, 0
    1, 17, 40, 9, 8 >>>>>>>>>>>>Must be deleted
    1, 17, 40, 5, 12
    1, 17, 40, 1, 16>>>>> Must be deleted
    1, 0, 25, 9, 40 >>>>>>>>>>>Qualifies
    1, 25, 3, 40, 6 >>>>>>>>>>>Must be deleted
    1, 25, 5, 40, 4 >>>>>>>>>>>Must be deleted

    Option Explicit
    
    Sub DelSpecificRow()
        Dim r As Range
        
        Range("H1").Select  ' you may change this to get your first data
        Range(ActiveCell, ActiveCell.End(xlDown)).Select
        For Each r In Selection
            If r.Value <> "*" & "," & " " & "0," & "*" Then
                r.EntireRow.Delete
            End If
        Next r
    End Sub
    Ricklou

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,054

    Re: If column dont contain 0 then delete row

    Here, try this:

    Sub DelSpecificRow()
        Dim r As Long
        Dim i As Long
        Dim x As Variant
        
        i = Range("H65536").End(xlUp).Row  ' You may change H to any other column
        For r = i To 1 Step -1
            x = Replace(Range("H" & r).Value, " ", "")
            If Left(x, 2) <> "0," And InStr(x, ",0,") = 0 And Right(x, 2) <> ",0" Then
                Range("H" & r).EntireRow.Delete
            End If
        Next r
    End Sub
    Last edited by zbor; 10-13-2012 at 05:31 AM.
    Never use Merged Cells in Excel

  3. #3
    Forum Contributor
    Join Date
    07-01-2012
    Location
    ZA
    MS-Off Ver
    Excel 2013
    Posts
    300

    Re: If column dont contain 0 then delete row

    Quote Originally Posted by zbor View Post
    Here, try this:

    Sub DelSpecificRow()
        Dim r As Long
        Dim i As Long
        Dim x As Variant
        
        i = Range("H65536").End(xlUp).Row  ' You may change H to any other column
        For r = i To 1 Step -1
            x = Replace(Range("H" & r).Value, " ", "")
            If Left(x, 2) = "0," Or InStr(x, ",0,") = 0 Or Right(x, 2) = ",0" Then
                Range("H" & r).EntireRow.Delete
            End If
        Next r
    End Sub
    This code works flawlessly...........!!!!Zbor thank you so much!!!!!!!!!!!!!!!! Solved

  4. #4
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: If column dont contain 0 then delete row

    as an option
    Sub DelSpecificRow()
    Dim r As Range, s As String
    Application.ScreenUpdating = 0
    s = "*, 0*": On Error Resume Next
    With Range("H1", Cells(Rows.Count, 8).End(xlUp))
        For Each r In .Cells
            If Not r Like s Then r = Empty
        Next r
        .SpecialCells(4).EntireRow.Delete
    End With
    Application.ScreenUpdating = 1
    End Sub
    Last edited by nilem; 10-13-2012 at 04:54 AM.

  5. #5
    Forum Contributor
    Join Date
    07-01-2012
    Location
    ZA
    MS-Off Ver
    Excel 2013
    Posts
    300

    Re: If column dont contain 0 then delete row

    Quote Originally Posted by nilem View Post
    as an option
    Sub DelSpecificRow()
    Dim r As Range, s As String
    Application.ScreenUpdating = 0
    s = "*, 0*": On Error Resume Next
    With Range("H1", Cells(Rows.Count, 8).End(xlUp))
        For Each r In .Cells
            If Not r Like s Then r = Empty
        Next r
        .SpecialCells(4).EntireRow.Delete
    End With
    Application.ScreenUpdating = 1
    End Sub
    Thank you for your code, I will try it aswell.......Zbor's did the trick though.

+ 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