Macro to combine rows based on last few characters from a column value

tmatt766

New Member
Joined
Nov 6, 2014
Messages
3
Hello,

For the past couple of weeks I've been working on a workbook to pull sales information from our database tool and put it in a presentable way that can be used as a meaningful report. I've pretty much finished (with lots of help from this forum) completing all the necessary macros to pull off the requirements for the book, but I'm stuck on one aspect. Usually I'm able to think through a problem with some semblance of a solution in my head but this one has me totally stumped, so let me know if you can think of something- I currently just have a macro that searches through the book deleting any row instances that have the European names, but I feel as if this is detrimental to the relevance of this report.

Problem:
Some of the products that are being pulled into the report have multiple names depending on the region that they are being sold in, for example in the US we may sell Fruit Peels 123, but if the same product was sold in Europe it would be called Food Peels 123. The characters at the end of the product are always the same (for example 123, x-123, etc.), which makes me think that a solution is feasible.

I need some way to loop through the product list looking for the last 4-5 characters of each product name, and then if they match any other entry in the list they would need to combine the two rows of data (sales volume, margin, etc.)... something akin to a sumif that employs a variable RIGHT() function for checking but the solution has to be in VBA (the raw data input from the database changes in size and entries each time it is refreshed based on the input dates and overwrites anything that gets in it's way so an embedded solution is pretty much the only option I see). Additionally this can't just be a simple find and replace function, because the European name encompasses two different category names in the US, for example: in Europe the name could be Food Peels 123, but in the US that could either be Fruit Peels 123 OR Vegetable Peels 123.

I made up some dummy data, in case my ramblings weren't clear:

Place People DollarsThings
a type 111120$14,423.009
b type 26104$11,962.004
b type 38196$9,374.002
a type x-411186$10,233.007
b type 511116$4,262.0010
b type 68154$2,849.005
b type 711147$19,778.005
a type 88103$14,920.0010
a type 92172$11,348.0010
a type 105124$17,357.0010
a type 119120$10,308.0010
b type 129193$8,840.008
a type 133173$6,708.002
b type 148140$11,713.0012
a type 152191$2,655.003
a type 165167$8,316.008
a type 1710171$8,018.003
b type 184152$7,282.002
b type 195112$6,037.002
a type 206171$2,736.006
b type 211173$4,542.001
a type x221133$6,557.003
a type 239177$8,367.004
b type 2412183$5,258.0010
a type 252130$19,937.006
a type 264199$7,559.001
a type 273189$13,650.0012
a type 289156$17,126.002
b type 2912138$4,633.003
c type 110130$11,255.0011
c type -45164$4,331.003
c type 811143$6,480.004
c type 125168$17,838.0010
c type 203183$9,803.005
c type x222158$11,514.008
c type 263187$16,061.008
c type 293189$10,135.008

<colgroup><col span="3"><col><col></colgroup><tbody>
</tbody>

Thanks for any help, and please let me know if I can clarify anything!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi and welcome to the MrExcel Message Board.

The first thing you need is a way of isolating the final number. This code should help:
Code:
' Note: Requires reference to "Microsoft VBScript Regular Expressions"

Sub RegEx1()

    ' Define variables
    Dim objRegExp As RegExp
    Dim objMatches As MatchCollection
    Dim iRow As Long
    
    ' Create RegEx Object
    Set objRegExp = CreateObject("vbscript.regexp")
    
    ' Set pattern to find any number of digits at end of string
    objRegExp.Pattern = "\d*$"
    
    ' Loop over column A
    For iRow = 2 To Range("A" & Rows.Count).End(xlUp).Row
        Set objMatches = objRegExp.Execute(Range("A" & iRow))
        If objMatches.Count = 1 Then Range("F" & iRow) = objMatches.Item(0).Value
    Next
    
End Sub

That will insert the final numbers into column F. Then you can use a pivottable, say, to group the data.

The code uses a VBScript Regular Expression to isolate the final numbers. If it finds one then it writes it to column F.
 
Upvote 0

Ok.
. You have explained it quite well!
. Just to be sure can you produce a similar table showing exactly wot you want based on that sample data (If you like you can reduce the sample data as long as it represents a good range of the different possibilities.)
. (I assume your first column should be titled product name)
. If you do that and no one else replies I will take a look later tonight.
Alan
 
Upvote 0
.......
. If you do that and no one else replies I will take a look later tonight.
Alan


...RickXl replied at the same time as me...!!! But I will still take a look later if you want to and if you supply the extra stuff i asked for
 
Upvote 0

Forum statistics

Threads
1,216,099
Messages
6,128,819
Members
449,469
Latest member
Kingwi11y

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