Add while deleting rows? Is this possible via VBA Macro

gisvpn used Ask the Experts™

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.


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015

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??

Top Expert 2015
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..

Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
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 Analyst
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 dirt
Most Valuable Expert 2017
Distinguished Expert 2018

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial