Results 1 to 7 of 7

Thread: Delete Rows Based on Conditions

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Question Delete Rows Based on Conditions

    Please forgive me if this is answered elsewhere-I've searched, but apparently don't know correct terms to search:

    I have data that I can block, copy, and then Paste into Excel 2007. (This is the only way to obtain this data). This is basically unformatted data that pastes multiple rows (~1500 rows) into Column A.

    Since I need to do this every week, is there a way I can Paste it into a worksheet/workbook that automatically/semi-automatically:
    1--Deletes all rows that begin with certain terms like: "Fill List" OR "Printed" OR "DOB:" OR "(et" OR "Aller"
    AND
    2--Deletes all rows that contain "Fill cycle"

    Once it does this, I think I can separate the strings into different cells by formulas that I can then send to a Pivot Table.

    I am a rookie at VBA -- I know how to open Visual Basic, but that's about all. If you could answer or direct me to a thread that does would be would be greatly appreciated.

    Thanks,
    AbiG

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi AbiG,

    Welcome to ExcelFox!!

    Try this code.

    Hit Alt+F11 > goto Insert > Module and paste this code there on the white pane. Alt+Q to close VBE and run kTest.

    Code:
    Option Explicit
    
    Sub kTest()
        
        Dim r       As Range
        Dim c       As Range
        Dim i       As Long
        Dim x, Flg  As Boolean, Skip As Boolean
        
        Const SearchKeysBeginsWith      As String = "Fill List,Printed,DOB:,(et,Aller" '<< add more words separated by comma
        Const SearchKeysContains        As String = "Fill Cycle" '<< add more words separated by comma
        
        Set r = Range("a1:a1500")   '<< adjust to suit
        
        Application.ScreenUpdating = 0
        
        With r
            x = Split(SearchKeysBeginsWith, ",")
    1:
            For i = 0 To UBound(x)
                .AutoFilter 1, IIf(Flg, "*" & x(i) & "*", x(i) & "*")
                On Error Resume Next
                Set c = .Cells(1).Offset(1).Resize(.Rows.Count - 1, 1).SpecialCells(12)
                On Error GoTo 0
                If Not c Is Nothing Then
                    c.EntireRow.Delete
                End If
            Next
            If Not Skip Then
                x = Split(SearchKeysContains, ",")
                Flg = True: Skip = True: GoTo 1
            End If
            .AutoFilter
        End With
        
        Application.ScreenUpdating = 1
        
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    I tried it and it seemed to work good, except I noticed A1 started with "Printed" and was still there. I also noticed a couple of other things, so I added them:

    onst SearchKeysBeginsWith As String = "Fill List,Printed,DOB:,(et,Aller,Generic,Rx #" '<< add more words separated by comma

    When I ran it again, it stopped. The Spreadsheet was Filtered, with only Row 1 showing, and the rest empty. I ran Debugger which highlighted this line: c.EntireRow.Delete

    Maybe I shouldn't have run it twice?

    Any ideas?
    Thanks
    AbiG

  4. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0
    OK--I pasted it into another worksheet and ran again. It did fine. Maybe it was running it twice that did it. So, I won't run it twice.

    In any case--it works great! Thanks a lot!
    AbiG

  5. #5
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    Thanks for the feedback.
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  6. #6
    Junior Member
    Join Date
    Dec 2018
    Posts
    5
    Rep Power
    0
    Dear Admin

    Thanks for the great code.
    I would like to know how to get the code to start from row A1 but also to remove rows that contain the dates expression with following symbol "/" such as 9/12/2018.
    Finally, how to move the numbers in rows <>"2018" at the end of the previous row. Any idea?

    Many thanks for the help and sharing knowledge

    Cheers
    Norman
    Last edited by Norman; 12-25-2018 at 06:37 PM.

  7. #7
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,455
    Rep Power
    10
    Hello Norman,
    Welcome to excelfox
    I am not too familiar with the code from Admin here, but I think adding a search key ( SearchKeysContains ) of a form something like this might be the sort of thing you would need
    "*" & "/" "*" & "/" & "*"
    The idea behind that is that many things in VBA code which take strings in them will read the * as meaning anything.
    So if a code looks for, pseudo like ...._
    Anything & "/" Anything & "/" & Anything
    _.... then a date of like 9/12/2018 would satisfy that.

    As I am not too familiar with the code in this Thread
    It might be easier for me to start a fresh in the other Thread that you have posted, ( http://www.excelfox.com/forum/showth...0851#post10851 )
    I will take a look at that Thread shortly and then post there

    Alan
    Last edited by DocAElstein; 12-26-2018 at 02:35 PM.
    A Folk, A Forum, A Fuhrer ….

Similar Threads

  1. Replies: 4
    Last Post: 03-22-2013, 01:47 PM
  2. Delete Rows
    By ayazgreat in forum Excel Help
    Replies: 2
    Last Post: 12-13-2012, 11:48 AM
  3. Replies: 1
    Last Post: 12-04-2012, 08:56 AM
  4. Delete unwanted rows & column
    By sanjeevi888 in forum Excel Help
    Replies: 1
    Last Post: 09-30-2012, 08:52 AM
  5. Delete Empty Rows
    By Rasm in forum Excel Help
    Replies: 4
    Last Post: 04-28-2011, 02:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •