button to delete row and update another row

MMarius

New Member
Hello,

I want if it contains a predefined value in a cell, to delete that row but not before to add the value that it has to the upper one, example below:


A B C
1 apples 30
2 orange 105
3 test 5
4 carrot 40
5 test 10


Will result like this:

On B3 it has "test" and value on C with 5, sum it with the upper one and delete complete the row afterwards (with test)
Same for B5.


I want like this:



A B C
1 apples 30
2 orange 110
3 carrot 50


I would like to have a button for this one.


Thank you
 

Joe4

MrExcel MVP, Junior Admin
See if this does what you want:
Code:
Sub MyDeleteMacro()

    Dim lr As Long, r As Long
    Dim str As String
    
    Application.ScreenUpdating = False
    
'   Designate column B value to delete
    str = "test"
    
'   Find last cell in column B with data
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Loop through all rows from bottom to top
    For r = lr To 2 Step -1
'       Check to see if row is one we are looking for
        If Cells(r, "B") = str Then
'           Add value from column C to row above
            Cells(r - 1, "C") = Cells(r - 1, "C") + Cells(r, "C")
'           Delete row
            Rows(r).Delete
        End If
    Next r
    
    Application.ScreenUpdating = True
    
End Sub
 

Some videos you may like

This Week's Hot Topics

Top