+ Reply to Thread
Results 1 to 8 of 8

Macro to Delete Rows based on Specific Conditions

Hybrid View

stockgoblin42 Macro to Delete Rows based on... 05-26-2013, 05:21 AM
JasperD Re: Macro to Delete Rows... 05-26-2013, 07:57 AM
stockgoblin42 Re: Macro to Delete Rows... 05-26-2013, 10:43 AM
JasperD Re: Macro to Delete Rows... 05-26-2013, 11:07 AM
stockgoblin42 Re: Macro to Delete Rows... 05-26-2013, 07:02 PM
JasperD Re: Macro to Delete Rows... 05-27-2013, 03:58 AM
stockgoblin42 Re: Macro to Delete Rows... 05-28-2013, 05:20 PM
JasperD Re: Macro to Delete Rows... 05-28-2013, 05:29 PM
  1. #1
    Forum Contributor stockgoblin42's Avatar
    Join Date
    05-26-2011
    Location
    vancouver, canada
    MS-Off Ver
    Excel 2010
    Posts
    222

    Macro to Delete Rows based on Specific Conditions

    Hi,

    I have a page of paralell lines. The slopes are listed in column A. I need to make a macro that keeps only the lines that match the first 3 slopes.

    Please see the attached worksheet.

    So, in the worksheet attached, the only lines that would be left after running the macro would be the ones with slopes of -0.04, -0.03 and -0.18. I've highlighted them for clarity.

    What's the best approach to this. Is it even possible?
    Attached Files Attached Files
    Last edited by stockgoblin42; 05-26-2013 at 06:57 AM.
    live logic & long prosper

  2. #2
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Macro to Delete Rows based on Specific Conditions

    Try this :

    Sub test()
    lastrow = Range("A65536").End(xlUp).Row
    For i = lastrow To 1 Step -1
    If Cells(i, 1).Value <> Range("A1").Value And Cells(i, 1).Value <> Range("A2").Value And Cells(i, 1).Value <> Range("A3").Value Then Cells(i, 1).EntireRow.Delete
    Next
    End Sub
    Please click the * below if this helps

  3. #3
    Forum Contributor stockgoblin42's Avatar
    Join Date
    05-26-2011
    Location
    vancouver, canada
    MS-Off Ver
    Excel 2010
    Posts
    222

    Re: Macro to Delete Rows based on Specific Conditions

    oH, oH, i found a special case. If the first 3 slopes match then this macro doesn't do exactly what I need

    to be more precise:
    I need to make a macro that keeps only the lines that match the first 3 "UNIQUE" slopes.

    can a simple change be made to accomodate this?

  4. #4
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Macro to Delete Rows based on Specific Conditions

    Try this :

    Sub test()
    x = 2
    a = Range("A1").Value
    
    Do Until b <> ""
    If Range("A" & x).Value <> a Then b = Range("A" & x).Value
    x = x + 1
    Loop
    Do Until c <> ""
    If Range("A" & x).Value <> a And Range("A" & x).Value <> b Then c = Range("A" & x).Value
    x = x + 1
    Loop
    
    lastrow = Range("A65536").End(xlUp).Row
    For i = lastrow To 1 Step -1
    If Cells(i, 1).Value <> a And Cells(i, 1).Value <> b And Cells(i, 1).Value <> c Then Cells(i, 1).EntireRow.Delete
    Next
    End Sub
    Please click the * below if this helps
    Last edited by JasperD; 05-26-2013 at 11:34 AM. Reason: slimmed the code a bit

  5. #5
    Forum Contributor stockgoblin42's Avatar
    Join Date
    05-26-2011
    Location
    vancouver, canada
    MS-Off Ver
    Excel 2010
    Posts
    222

    Re: Macro to Delete Rows based on Specific Conditions

    Excellent thanks. Just doesn't like it if there are less than 3 unique slopes.
    Last edited by stockgoblin42; 05-26-2013 at 09:28 PM.

  6. #6
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Macro to Delete Rows based on Specific Conditions

    I didn't think you'd have less than 3 unique slopes.

    Try amended code below :

    Sub test()
    lr = Range("A65536").End(xlUp).Row
    x = 2
    a = Range("A1").Value
    b = ""
    c = ""
    
    Do Until b <> "" Or x = lr
    If Range("A" & x).Value <> a Then b = Range("A" & x).Value
    x = x + 1
    Loop
    
    If x = lr Then GoTo nxt
    
    Do Until c <> "" Or x = lr
    If Range("A" & x).Value <> a And Range("A" & x).Value <> b Then c = Range("A" & x).Value
    x = x + 1
    Loop
    
    nxt:
    If b = "" Then GoTo just1
    If c = "" Then GoTo just2
    
    For i = lr To 1 Step -1
    If Cells(i, 1).Value <> a And Cells(i, 1).Value <> b And Cells(i, 1).Value <> c Then Cells(i, 1).EntireRow.Delete
    Next
    Exit Sub
    
    just1:
    For i = lr To 1 Step -1
    If Cells(i, 1).Value <> a Then Cells(i, 1).EntireRow.Delete
    Next
    Exit Sub
    
    just2:
    For i = lr To 1 Step -1
    If Cells(i, 1).Value <> a And Cells(i, 1).Value <> b Then Cells(i, 1).EntireRow.Delete
    Next
    End Sub
    Please click the * below if this helps

  7. #7
    Forum Contributor stockgoblin42's Avatar
    Join Date
    05-26-2011
    Location
    vancouver, canada
    MS-Off Ver
    Excel 2010
    Posts
    222

    Re: Macro to Delete Rows based on Specific Conditions

    Nice. Wow, you can do almost anything with programming, hey?

    I'm having problems with some data having way too many parallel matches though.

    I'm interested in seeing the first 3 unique slopes and instead of all their matches, just the first one? So only 6 lines would be left.

    Is that easy to do?

  8. #8
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: Macro to Delete Rows based on Specific Conditions

    Since you specifically mention three slopes, I adjusted the code I gave you in the first place. If you want it to work for 1 or 2 slopes, just adjust the other code yourself (is good exercise for you )

    Sub test()
    x = 2
    a = Range("A1").Value
    
    Do Until b <> ""
    If Range("A" & x).Value <> a Then b = Range("A" & x).Value
    x = x + 1
    Loop
    Do Until c <> ""
    If Range("A" & x).Value <> a And Range("A" & x).Value <> b Then c = Range("A" & x).Value
    x = x + 1
    Loop
    
    lastrow = Range("A65536").End(xlUp).Row
    k = 0
    l = 0
    m = 0
    
    For i = 1 To lr 
    If Cells(i, 1).Value <> a And Cells(i, 1).Value <> b And Cells(i, 1).Value <> c Then Cells(i, 1).EntireRow.Delete
    If Cells(i, 1).Value = a Then
    k = k + 1
    If k > 1 then Cells(i, 1).EntireRow.Delete
    End if
    If Cells(i, 1).Value = b Then
    l = l + 1
    If l > 1 then Cells(i, 1).EntireRow.Delete
    End if
    
    If Cells(i, 1).Value = c Then
    m = m + 1
    If m > 1 then Cells(i, 1).EntireRow.Delete
    End if
    
    Next
    End Sub
    Again, if this helps you, please click the * below

+ 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