Add while deleting rows? Is this possible via VBA Macro


I have the attached workbook which in column 'p' has a number. At the end of each number there is a -xx where the xx represents a two digit number. I am only interested in the rows with -11 in however this is where is gets tricky. I would like the amount and hours that appear in column X and Y respectively to be added to the amount and hours for the -11 row; so for example if I had:

Number                  Amount Hours
FR-3418432-11        $10         50
FR-3418432-12        $10         50
FR-3418432-13        $10         50

I would like to delete FR-3418432-12 and FR-3418432-13 but before deleting add the hours and amount for these two rows to FR-3418432-11 so after it would say:

Number                  Amount Hours
FR-3418432-11        $30         150

The attached spreadsheet provides an example with the way in which the data tends to appear. I would be grateful for anyone's input to this as always.


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
Quick question will be the one which has -11 entry will always be the first before rest or it can be later as well??

What i mean -12 and -13 and then -11 or it will be always -11 ,-12 and -13??

Saurabh Singh TeotiaCommented:
If the answer of the above question is Yes..then you can run the following code which will do what you are looking for..

Sub combinedata()
    Dim rng As Range, cell As Range
    Dim r As Range, lrow As Long
    Dim k As Long, i As Long

    lrow = Cells(Cells.Rows.Count, "P").End(xlUp).Row

    Set rng = Range("P2:P" & lrow)

    For Each cell In rng
        k = 0
        i = 0
        If Right(cell.Value, 2) = 11 Then

            k = Application.Evaluate("=SUMPRODUCT((LEFT(P2:P" & lrow & ",LEN(P2:P" & lrow & ")-3)=LEFT(P" & cell.Row & ",LEN(P" & cell.Row & ")-3))*(X2:X" & lrow & "))")
            i = Application.Evaluate("=SUMPRODUCT((LEFT(P2:P" & lrow & ",LEN(P2:P" & lrow & ")-3)=LEFT(P" & cell.Row & ",LEN(P" & cell.Row & ")-3))*(y2:y" & lrow & "))")

            cell.Offset(0, 8).Value = k
            cell.Offset(0, 9).Value = i

            If r Is Nothing Then
                Set r = cell
                Set r = Union(r, cell)
            End If

        End If

    Next cell

    If Not r Is Nothing Then r.EntireRow.Delete

End Sub

Open in new window

Enclosed is your workbook for your reference..I have run the code in the worksheet called Data2 to shows you the revised data post macro has run..


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
I had a different approach to achieve this...
Please try the below code to get the desired output.
You may click the button on the Data sheet to run the code.
Sub GetAmountAndHours()
Dim Srng As Range, rng As Range, Drng As Range
Dim lr As Long, i As Long, r As Long
Dim TAmount As Long, THour As Long

Application.ScreenUpdating = False
lr = Cells(Rows.Count, "P").End(xlUp).Row

For i = lr To 2 Step -2
Set Srng = Range("P2:P" & lr)
    If Cells(i, "P") <> "" Then
        With Srng
        Set rng = .Find(what:=Left(Cells(i, "P"), 2), _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlFormulas, _
                            LookAt:=xlPart, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
            If Not rng Is Nothing Then
                r = rng.Row
                    TAmount = TAmount + Cells(rng.Row, "X")
                    THour = THour + Cells(rng.Row, "Y")
                    If r <> rng.Row Then
                        If Drng Is Nothing Then
                            Set Drng = Cells(rng.Row, "P")
                            Set Drng = Union(Drng, Cells(rng.Row, "P"))
                        End If
                    End If
                    Set rng = .FindNext(rng)
                Loop While Not rng Is Nothing And r <> rng.Row
                Cells(r, "X") = TAmount
                Cells(r, "Y") = THour
                If Not Drng Is Nothing Then Drng.EntireRow.Delete
                Set Drng = Nothing
                TAmount = 0
                THour = 0
                r = 0
            End If
        End With
    End If
Next i
Application.ScreenUpdating = True
MsgBox "Finished!", vbInformation
End Sub

Open in new window

Rob HensonFinance AnalystCommented:
Alternative without VBA:

In a separate column add the following formula:


Copy down as required. This will give just the first 10 characters, ie without the -11 or -12 or -13

Then create a pivot table on the data using the new column as a row header.

Rob H
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.