vba to merge into one amount

Can an expert help me out with this please.

In my spread sheet there are codes in column ‘C’ Some of these I do not want to touch, others I need to merge the amounts.

So, I think I need VBA code where I tell it what codes to look at.

In the attached spread sheet you will see I have ‘IC123,4 and 5 which I do not want to touch. I have codes 700001 through 700021 which I need the VBA to work on.

I need to look at each code and where there are more than one of the same code, total them, put the total in the first occurrence and delete the other rows.

So i.e. 700001 has 4 amounts they total 65.00 so replace 15 [first amount] with 65.00 and delete rows with 20, 25 and 5. Then do this for all other codes starting with  7 in column ‘C’

17/08/2016      700001      15
17/08/2016      700001      20
17/08/2016      700001      25
17/08/2016      700001      5

Many thanks
Find-and-merge.xlsx
JagwarmanAsked:
Who is Participating?
 
MacroShadowConnect With a Mentor Commented:
There are changes in this file that you didn't mention...
Run the macro in the attached file, it does exactly as you ask.
Find-and-Move-JK.xlsm
0
 
Martin LissOlder than dirtCommented:
What do you mean by "starting with  7 in column ‘C’"?

Do you mean that you don't want to do anything with rows 1 to 13, and you want to start merging at row 14?

Will the first 13 rows always be there? In other words can there be less or more rows that you don't want to touch?
0
 
MacroShadowCommented:
Please try this:
Sub Demo()

    Dim lngRow As Long
    Dim lngLastRow As Long

    lngLastRow = Worksheets("Start").Columns("C").Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row

    With Worksheets("Start").Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("C14"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange Range("C14:D" & lngLastRow)
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    For lngRow = lngLastRow To 2 Step -1
        If IsNumeric(Cells(lngRow, 3)) Then
            If Cells(lngRow, 3).Value = Cells(lngRow - 1, 3).Value Then
                Cells(lngRow, 4).Value = Cells(lngRow, 4).Value + Cells(lngRow - 1, 4).Value
                Cells(lngRow - 1, 4).EntireRow.Delete
            End If
        End If
    Next

End Sub

Open in new window

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Roy CoxGroup Finance ManagerCommented:
Why not use a PivotTable? A PivotTable requires no VBA and can be refreshed simply

Here's an introduction to PivotTables
Find-and-merge.xlsx
0
 
JagwarmanAuthor Commented:
When I said "starting with  7 in column ‘C’"?  that meant any code that starts with a 7, like 700001,700002 etc. I cannot use Pivot for various reasons otherwise I would.

But one thing I forgot in my original request was that the narrative in column 'F' must also be the same so for instance:

700002 would be merged to give a total of 71.92
700005 would not be merged

N/A      12/08/2016      700002       £65.10      N/A      DM6 XYZ MCE  10-08-16
N/A      12/08/2016      700002       £6.82      N/A      DM6 XYZ MCE  10-08-16
N/A      12/08/2016      700005       £20.50      N/A      DM6 ME1 STMT 10-08-16
N/A      12/08/2016      700005       £5.00      N/A      DM6 cards ME1 11-08-16

sorry for confusion
0
 
MacroShadowCommented:
Try this:
Sub Demo()

    Dim lngRow As Long
    Dim lngLastRow As Long

    lngLastRow = Worksheets("Start").Columns("C").Cells.Find("*", SearchOrder:=xlByRows, LookIn:=xlValues, SearchDirection:=xlPrevious).Row

    With Worksheets("Start").Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("C14"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange Range("C14:D" & lngLastRow)
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

    For lngRow = lngLastRow To 2 Step -1
        If Left(Cells(lngRow, 3), 1) = 7 Then
            If Cells(lngRow, 3).Value = Cells(lngRow - 1, 3).Value And Cells(lngRow, 6).Value = Cells(lngRow - 1, 6) Then
                Cells(lngRow, 4).Value = Cells(lngRow, 4).Value + Cells(lngRow - 1, 4).Value
                Cells(lngRow - 1, 4).EntireRow.Delete
            End If
        End If
    Next

End Sub

Open in new window

0
 
JagwarmanAuthor Commented:
sorry for delay in getting back. Thanks for your solutions however they do not appear to merge the amounts, so if I have

N/A      12/08/2016      700002       £65.10      N/A      DM6 XYZ MCE  10-08-16
 N/A      12/08/2016      700002       £6.82      N/A      DM6 XYZ MCE  10-08-16

I should end up with one row

N/A      12/08/2016      700002       £71.92 N/A      DM6 XYZ MCE  10-08-16

Regards
0
 
MacroShadowCommented:
I tested it on the file you uploaded and it worked as expected.
If the layout is diferent in your production file you will have to addapt the code.
0
 
JagwarmanAuthor Commented:
I have tested and re-tested and they remove rows but do not keep the totals.

so as I said above:

N/A      12/08/2016      700002       £65.10      N/A      DM6 XYZ MCE  10-08-16
  N/A      12/08/2016      700002       £6.82      N/A      DM6 XYZ MCE  10-08-16

 I should end up with one row

 N/A      12/08/2016      700002       £71.92 N/A      DM6 XYZ MCE  10-08-16

but I end up with

 N/A      12/08/2016      700002       £65.10 N/A      DM6 XYZ MCE  10-08-16
0
 
MacroShadowCommented:
Can you upload a sample file?
0
 
JagwarmanAuthor Commented:
As requested
Find-and-Move-JK.xlsx
0
 
JagwarmanAuthor Commented:
Many thanks and apologies for any confusion.
0
All Courses

From novice to tech pro — start learning today.