button to delete row and update another row

MMarius

Board Regular
Joined
Sep 15, 2015
Messages
60
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
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
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
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,215
Members
448,554
Latest member
Gleisner2

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