Solved

vba to merge into one amount

Posted on 2016-08-14
12
46 Views
Last Modified: 2016-08-17
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
0
Comment
Question by:Jagwarman
12 Comments
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
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
 
LVL 17

Expert Comment

by:Roy_Cox
Comment Utility
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
 

Author Comment

by:Jagwarman
Comment Utility
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
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
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
 

Author Comment

by:Jagwarman
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
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
 

Author Comment

by:Jagwarman
Comment Utility
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
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
Can you upload a sample file?
0
 

Author Comment

by:Jagwarman
Comment Utility
As requested
Find-and-Move-JK.xlsx
0
 
LVL 26

Accepted Solution

by:
MacroShadow earned 500 total points
Comment Utility
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
 

Author Closing Comment

by:Jagwarman
Comment Utility
Many thanks and apologies for any confusion.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now