Conditional copy and paste, then delete row

dockingsched

New Member
Joined
Nov 7, 2019
Messages
3
Hello all, Bit of an amateur trying to learn more on the use of Macros. Hoping for some assistance with this issue: I want to be able to search through my data to find rows where Column D displays "Negative".If it doesn't display "Negative", just move on to the next row. If it does display "Negative", i want to check if the value in Column A of this row is equal to the value in Column A of the previous row. I also want to check if the value in Column C of this row is equal to the value in Column C of the previous row. If either condition is not met, then just move on to the next row that displays "Negative". However, if both conditions are met, then I want to cut the values in Columns T through V of the row that displays "negative" and paste them onto Columns T through V of the previous row, then if possible delete the row that displays "Negative" where we just cut the values from. Hope I was able to explain that well enough and thank you for any assistance or guidance that can be provided.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi & welcome to MrExcel.
A few questions
Does col D have the word Negative on it's own in a cell?
Can you ever have two consecutive rows with Negative in col D? If so what should happen?
Do cols T:V contain values or formulae?
 
Upvote 0
Hi & welcome to MrExcel.
A few questions
Does col D have the word Negative on it's own in a cell?
Can you ever have two consecutive rows with Negative in col D? If so what should happen?
Do cols T:V contain values or formulae?


-Does col D have the word Negative on it's own in a cell?

Yes, the word Negative would be the only item inside the cell.

-Can you ever have two consecutive rows with Negative in col D? If so what should happen?

No, there won't be two consecutive rows with "Negative" in col D. Also, if the word negative is in the first row, it should just keep going to the next row since there's no previous row to compare data with.

-Do cols T:V contain values or formulas?

They contain numeric values, no formulas.
 
Upvote 0
Wrong
 
Last edited:
Upvote 0
Thanks for that, how about
Code:
Sub dockingsched()
    Dim Rng As Range, Cl As Range, DelRng As Range
    
    With Range("D2", Range("D" & Rows.Count).End(xlUp))
        .Replace "Negative", "=xxxNegative", xlWhole, , False, , False, False
        On Error Resume Next
        Set Rng = .SpecialCells(xlFormulas, xlErrors)
        On Error GoTo 0
        .Replace "=xxxNegative", "Negative", xlWhole, , False, , False, False
    End With
    If Rng Is Nothing Then MsgBox "No Negatives": Exit Sub
    For Each Cl In Rng
        If Cl.Offset(, -3) = Cl.Offset(-1, -3) And Cl.Offset(, -1) = Cl.Offset(-1, -1) Then
            Cl.Offset(-1, 16).Resize(, 3).Value = Cl.Offset(, 16).Resize(, 3).Value
            If DelRng Is Nothing Then Set DelRng = Cl Else Set DelRng = Union(DelRng, Cl)
        End If
    Next Cl
    If Not DelRng Is Nothing Then DelRng.EntireRow.Delete
End Sub


EDIT:
I removed the code from post#4 as it was wrong.
 
Upvote 0
Thanks for that, how about
Code:
Sub dockingsched()
    Dim Rng As Range, Cl As Range, DelRng As Range
    
    With Range("D2", Range("D" & Rows.Count).End(xlUp))
        .Replace "Negative", "=xxxNegative", xlWhole, , False, , False, False
        On Error Resume Next
        Set Rng = .SpecialCells(xlFormulas, xlErrors)
        On Error GoTo 0
        .Replace "=xxxNegative", "Negative", xlWhole, , False, , False, False
    End With
    If Rng Is Nothing Then MsgBox "No Negatives": Exit Sub
    For Each Cl In Rng
        If Cl.Offset(, -3) = Cl.Offset(-1, -3) And Cl.Offset(, -1) = Cl.Offset(-1, -1) Then
            Cl.Offset(-1, 16).Resize(, 3).Value = Cl.Offset(, 16).Resize(, 3).Value
            If DelRng Is Nothing Then Set DelRng = Cl Else Set DelRng = Union(DelRng, Cl)
        End If
    Next Cl
    If Not DelRng Is Nothing Then DelRng.EntireRow.Delete
End Sub


EDIT:
I removed the code from post#4 as it was wrong.

this appears to be working perfectly, thanks a lot.
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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