+ Reply to Thread
Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    05-31-2008
    Location
    chicago,Illinois,USA
    Posts
    26

    Delete rows, multiple conditions

    Hello,

    I need help with the following conditions. I have attached a sample file with desired results.
    Conditions required to delete row:

    1 Delete Row when cell E is blank (example: Row 6 will be deleted as E6 is blank)
    2 First character in column 'C' is an alphabet (for example: Row 5 will be deleted as the first character in the cell is an alphabet
    3 First character in column 'B' is an alphabet (for example: Row 29 will be deleted as the first character in the cell is an alphabet
    Special Notes : All characters in column 'B' are not in number format (ex: 555-9999 is general)
    There are more than 1500 rows of data that need to be sorted with the above conditions

    Thanks
    Attached Files Attached Files
    Last edited by rm7302; 07-22-2009 at 09:23 AM.

  2. #2
    Forum Moderator shg's Avatar
    Join Date
    06-21-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007
    Posts
    25,134

    Re: Delete rows, multiple conditions

    Try this:
    Code:
    Sub x()
        Dim iRow    As Long
    
        For iRow = Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
            If Cells(iRow, "B").Text Like "[A-Z][a-z]*" Or _
               Cells(iRow, "C").Text Like "[A-Z][a-z]*" Or _
               Len(Cells(iRow, "E").Text) = 0 Then
                Rows(iRow).Delete
            End If
        Next iRow
    End Sub
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Guru
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2003, 2007.
    Posts
    1,413

    Re: Delete rows, multiple conditions

    rm7302,

    Here you go.

    shg - nicely done.


    Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

    Code:
    Option Explicit
    Sub DeleteRowsOnCondition()
    Dim a As Long
    Application.ScreenUpdating = False
    On Error Resume Next
    Range("E2:E" & Cells(Rows.Count, 1).End(xlUp).Row).SpecialCells(xlBlanks).EntireRow.Delete
    On Error GoTo 0
    For a = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
      If Not IsNumeric(Left(Cells(a, 2), 1)) Or Not IsNumeric(Left(Cells(a, 3), 1)) Then
        Rows(a).EntireRow.Delete
      End If
    Next a
    Application.ScreenUpdating = True
    End Sub

    Then run the "DeleteRowsOnCondition" macro.
    Have a great day,
    Stan
    stanleydgromjr
    Windows Vista Business, Excel 2003 and 2007

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,495

    Re: Delete rows, multiple conditions

    Hello rm7302,

    Here is another method. You can change the worksheet name and starting cell if you need to. They are marked in red. The macro will find the last entry in starting column automatically.
    Code:
    Sub DeleteRows()
    
      Dim Data As Variant
      Dim Item As Variant
      Dim Rng As Range
      Dim RngEnd As Range
      
        Set Rng = Worksheets("Sheet1").Range("A2")
        Set RngEnd = Rng.Parent.Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Rng.Parent.Range(Rng, RngEnd))
        
          Data = Rng.Resize(ColumnSize:=5).Value
          
          Application.ScreenUpdating = False
            For I = UBound(Data, 1) To 1 Step -1
              If Data(I, 5) = "" Or Data(I, 3) Like "[a-zA-Z]*" Or Data(I, 2) Like "[a-zA-Z]*" Then
                Rng.Rows(I).EntireRow.Delete
              End If
            Next I
          Application.ScreenUpdating = True
          
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    05-31-2008
    Location
    chicago,Illinois,USA
    Posts
    26

    Re: Delete rows, multiple conditions

    shg,

    Thanks a lot for the code. If possible can you explain me the code.
    Leith Ross and stanleydgromjr thanks too. I will try your solution too.
    Also, where can I learn more about add-ins and personal.xls files.

  6. #6
    Registered User
    Join Date
    05-31-2008
    Location
    chicago,Illinois,USA
    Posts
    26

    Re: Delete rows, multiple conditions

    shg, Leith Ross and stanleydgromjr,

    The code works fine on the sample file but when I am using the same code (copy/paste) on my original file it fails. Anything else I need to do for this to work? Thanks again for all your help. All you guys are awesome !!!!!

  7. #7
    Valued Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: Delete rows, multiple conditions

    At a guess you either pasted into the wrong location, or else your sample file doesn't accurately represent your original file.
    In what way does it fail, do you receive any error message?
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  8. #8
    Registered User
    Join Date
    05-31-2008
    Location
    chicago,Illinois,USA
    Posts
    26

    Re: Delete rows, multiple conditions

    Phil_V,

    I pasted into 'ThisWorkbook' location on my original file. It did not delete the rows I wanted but this worked fine on the sample file. Also, I tried pasting the code in the sheet where I wanted the rows to be deleted. I saved and ran the macro, did not work again. Let me know, if I am doing anything wrong. Thanks for the reply.

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,495

    Re: Delete rows, multiple conditions

    Hello rm7302,

    The code needs to be placed in Standard VBA module. Here is how to add the macro to your project.

    b]Adding the Macro[/b]
    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time.
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel.

    To Run the Macro...
    To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  10. #10
    Registered User
    Join Date
    05-31-2008
    Location
    chicago,Illinois,USA
    Posts
    26

    Re: Delete rows, multiple conditions

    Thanks a lot Leith Ross. It worked, I was missing some data in the first column that was the reason for failing.You are truly a genius. Can I contact you directly for future problems ? Also, can you suggest me some good books on VBA and macros. I am just getting better.

    Thanks a ton.

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