+ Reply to Thread
Results 1 to 5 of 5

Thread: Macro to delete rows if certain condition is met

  1. #1
    Registered User
    Join Date
    11-10-2011
    Location
    New York, NY
    MS-Off Ver
    Excel 2007
    Posts
    9

    Macro to delete rows if certain condition is met

    Hello guys I am trying to build a macro that will delete the entire row if a certain value appears in a certain column. For example lets say it goes down column F, I will need the macro to delete entire rows if there is no "1" present. Is creating a macro like this possible. Thanks in advance for the help, I've also posted a sample of the data.
    Attached Files Attached Files

  2. #2
    Extremely Helpful member
    Join Date
    12-23-2006
    Location
    Belgium
    MS-Off Ver
    XL2003
    Posts
    6,127

    Re: Macro to delete rows if certain condition is met

    You can probably tweak the solution prvided by Mudraker here ( I don't have access to XL right now)
    Quoting entire posts clutters the forum and makes threads hard to read !

    If you are pleased with a member's answer then use the Star icon to rate it

    Click here to see forum rules

  3. #3
    Valued Forum Contributor Charles's Avatar
    Join Date
    02-10-2004
    Location
    Biloxi
    MS-Off Ver
    Windows 7, Excel 2003,2007 & Mac2011
    Posts
    657

    Re: Macro to delete rows if certain condition is met

    HI,

    The following code may help. Copy to a module and change sheet name to the sheet name in your file.
    Test on copy only.

    Sub FilterDeleteNon1()
    Application.ScreenUpdating = False
    Dim VisRng As Range, cell As Range
    Dim Myval As Integer
    Dim lrow As Long
    lrow = Sheets("Sheet2").Range("G65536").End(xlUp).Row
        Range("A1:G1").Select
            Selection.AutoFilter
            With Selection
                .AutoFilter Field:=7, Criteria1:="<> 1"    '' this set the filtered data for the value
            End With
                With Worksheets("Sheet2").AutoFilter.Range
    
                Set VisRng = .Resize(.Rows.Count - 1, 1).Offset(1, 0) _
                        .Cells.SpecialCells(xlCellTypeVisible)
                '' make sure you have more than 1 row  ''
                Myval = .Range("g2:g" & lrow).SpecialCells(xlCellTypeVisible).Count
                If Myval >= "2" Then
                    Range(Cells(VisRng.Offset(1, 0).Row, 1), Cells(Range("A65536").End(xlUp).Row, 4)).EntireRow.Delete
                End If
                End With
        Selection.AutoFilter
    End Sub
    Charles

    There are other ways to do this, this is but 1 !
    Be Sure you thank those who helped.
    IF YOU'RE SATISFIED BY ANY MEMBERS RESPONSE TO YOUR ISSUE PLEASE USE THE STAR ICON AT THE BOTTOM LEFT OF THE POST UNDER THEIR NAME.

  4. #4
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,376

    Re: Macro to delete rows if certain condition is met

    Another version-
    Option Explicit
    Dim lrow As Long
    Dim i As Long
    
    Sub delete_rows()
    
    lrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    For i = lrow To 1 Step -1
        If Range("G" & i).Value <> 1 Then
            Rows(i & ":" & i).Delete
            lrow = lrow - 1
        End If
    Next i
    
    End Sub
    I am not able to delete the rows containing #VALUE!. Maybe you can take care of it when you have a formula(i guess vlookup) to make it "" or blank instead of #VALUE!.
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  5. #5
    Valued Forum Contributor
    Join Date
    07-20-2011
    Location
    mysore
    MS-Off Ver
    Excel 2003
    Posts
    421

    Re: Macro to delete rows if certain condition is met

    I have slightly modified the code of Sri arlu1201. This works ok. No modifications necessary. Convey your findings.



    Option Explicit
    Dim lrow As Long
    Dim i  As Long
    
    Sub delete_rows()
    lrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    
    For i = lrow To 1 Step -1
        If IsError(Range("G" & i).Value) Then
            Rows(i & ":" & i).Delete
            lrow = lrow - 1
        ElseIf Range("G" & i).Value <> 1 Then
            Rows(i & ":" & i).Delete
            lrow = lrow - 1
            
        End If
    Next i
    
    End Sub
    Last edited by kvsrinivasamurthy; 12-20-2011 at 11:34 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0