+ Reply to Thread
Results 1 to 4 of 4

Deleting multiple rows with specific criteria in multple columns

  1. #1
    Registered User
    Join Date
    06-12-2012
    Location
    Eastvale, California
    MS-Off Ver
    Excel 2007
    Posts
    10

    Deleting multiple rows with specific criteria in multple columns

    I want to delete rows with specific criteria, for example for column A I want to delete any rows containing "C", in column E I want to delete rows containing "AB", column F I want to delete rows containing "CE", in column G I want to delete rows containing "ZZ". I've tried a code that worked but only for 1 column & 1 criteria but I'm trying to create a macro to delete multiple columns with different criteria. Here's the code I tried and worked but I need it for multiple columns:

    Sub Union_Example()
    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim rng As Range

    With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    End With

    'We use the ActiveSheet but you can replace this with
    'Sheets("MySheet")if you want
    With ActiveSheet

    'Set the first and last row to loop through
    Firstrow = .UsedRange.Cells(1).Row
    Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row

    'We loop from Lastrow to Firstrow (bottom to top)
    For Lrow = Lastrow To Firstrow Step -1

    'We check the values in the A column in this example
    With .Cells(Lrow, "A")

    If Not IsError(.Value) Then

    If .Value = "C" Then
    'This will delete each row with the Value "C"
    'in Column A, case sensitive.

    If rng Is Nothing Then
    Set rng = .Cells
    Else
    Set rng = Application.Union(rng, .Cells)
    End If
    End If

    End If
    End With

    Next Lrow

    End With

    'Delete all rows in one time
    If Not rng Is Nothing Then rng.EntireRow.Delete

    ActiveWindow.View = ViewMode
    With Application
    .ScreenUpdating = True
    .Calculation = CalcMode
    End With

    End Sub

    please help.

    cb10

  2. #2
    Registered User
    Join Date
    05-16-2012
    Location
    Austin, Texas
    MS-Off Ver
    Excel 2007
    Posts
    21

    Re: Deleting multiple rows with specific criteria in multple columns

    Try:
    Please Login or Register  to view this content.

  3. #3
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Deleting multiple rows with specific criteria in multple columns

    Hi cb10,

    Welcome to the forum!!

    Try this (initially on a copy of your data as the results cannot be undone):

    Please Login or Register  to view this content.
    You also need to wrap your code with the appropriate tags as JoeMo and I have done.

    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  4. #4
    Registered User
    Join Date
    06-12-2012
    Location
    Eastvale, California
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: Deleting multiple rows with specific criteria in multple columns

    Hi JoMoe & Trebor76, thanks to both of you. I got the 2nd one to work for me but I know it's only because I'm a beginner at all this. I kept getting an error for the 1st that I was unable to correct and identify the errors but I'm working on it. I really appreciate it you saved me a lot of time and headaches.

+ 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