Can Excel execute this complex value recheck with rows being deleted?

Coyotex3

Active Member
Joined
Dec 12, 2021
Messages
496
Office Version
  1. 365
Platform
  1. Windows
1640232867081.png


So, I would like to tell excel to look at the value in row(Where it says Cash Available) in this case "H16" and if the value is less than the value above it "H15" to delete where Range("D") = "Okinawa" and Range("A") = OsaCon, starting by oldest date "Range("E") reexamine to see if value in "H16" is greater than the value in "H15."

Thank you.
 
Sorry, didn't realize you changed the name:

VBA Code:
Sub DeleteRowPerTests()
'
    Dim OldestDate          As Date
    Dim PreviousRow         As Long
    Dim LastRowInColumnG    As Long
    Dim OffsetColumn        As Long
    Dim OffsetRow           As Long
    Dim RowToDelete         As Long
    Dim StartRowData        As Long
    Dim cell                As Range
'
    StartRowData = 6
'
    LastRowInColumnG = Range("G" & Rows.Count).End(xlUp).Row                                ' Get last row # used in column G
'
StartOfForEachLoop:
    For Each cell In Range("G" & StartRowData & ":G" & LastRowInColumnG)                    ' Loop through the G column
        OffsetColumn = 0
        OffsetRow = 0
'
        If cell.Value = "Cash Availables" And cell.Offset(OffsetRow, OffsetColumn + 1) <> vbNullString And _
            cell.Offset(OffsetRow, OffsetColumn + 1) < cell.Offset(OffsetRow - 1, OffsetColumn + 1) And _
            cell.Offset(OffsetRow - 2, OffsetColumn - 3) = "Okinawa" _
            And cell.Offset(OffsetRow - 2, OffsetColumn - 6) = "OsaCon" Then                '   If all conditions are met then ...
'
            OldestDate = cell.Offset(OffsetRow - 2, OffsetColumn - 2).Value2                '       Save the OldestDate
            RowToDelete = cell.Offset(OffsetRow - 2, OffsetColumn - 2).Row                  '       Save the RowToDelete for the OldestDate
'
            For PreviousRow = 3 To RowToDelete - StartRowData
                If cell.Offset(OffsetRow - PreviousRow, OffsetColumn - 3) = "Okinawa" And _
                    cell.Offset(OffsetRow - PreviousRow, OffsetColumn - 6) = "OsaCon" Then  '           If both other conditions are met then ...
                        If cell.Offset(OffsetRow - PreviousRow, OffsetColumn - 2).Value2 < OldestDate Then  '               If older date found then ...
                            OldestDate = cell.Offset(OffsetRow - PreviousRow, OffsetColumn - 2).Value2  '                       Save the Date as the OldestDate
                            RowToDelete = cell.Offset(OffsetRow - PreviousRow, OffsetColumn - 2).Row    '                   Save the RowToDelete for the OldestDate
                        End If
                Else
                    Exit For                                                                '               Exit for loop
                End If
            Next
'
            Rows(RowToDelete).EntireRow.Delete                                              '       Delete the row with the earliest date that matches all criteria
'
            GoTo StartOfForEachLoop                                                         '       Go back to beginning of loop and check again
        End If
    Next
End Sub
HOLY SMOKES!!! This seems to work. I will test it on a larger sheet and I will have to edit it a bit as I will need this loop to go to different ranges and look for different things in each range.

How you guys are able to figure this out is beyond me!
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Let us know if you need anything else.
I've been examining this code, and man I think this is bonkers. Even with your explanations on the comments, I'm blown away by everything that is going on!
 
Upvote 0
@Coyotex3 am glad it worked for you. One of the reasons I comment my code is so the recipient can sort of get a grasp of what the lines of code are attempting to do. As opposed to just running code and not having a clue how or why the code works, or doesn't, for that matter.
 
Upvote 0

Forum statistics

Threads
1,214,586
Messages
6,120,402
Members
448,958
Latest member
Hat4Life

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top