Combining cells from different rows under multiple conditions

Magoosball

Board Regular
Joined
Jun 4, 2017
Messages
70
Office Version
  1. 365
I'm trying to write a VBA script that will turn table A into Table B


Table A:

NameAnimal
MikeDog
MikeCat
MikeMouse
MikeRat
MikeChicken
MikeSnake
MikeBear
AprilMouse
AprilChicken
AprilSnake
JamesBengal Cat
JamesRat
ThorDog
ThorCat
ThorRat
ThorSnake
ThorBear

<tbody>
</tbody>

Table B

NameAnimal
MikeDog, Cat, Rat
Mike
MikeMouse
Mike
MikeChicken
MikeSnake
MikeBear
AprilMouse
AprilChicken
AprilSnake
JamesBengal Cat, Rat
James
ThorCat, Rat
Thor
ThorSnake
ThorBear

<tbody>
</tbody>


In summary: For each persons name in row A, I want to combine the cells, separated by a comma, if the cell contains the words: "Dog", "Cat", or "Rat"

Thank you in advance!!
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Code:
[color=darkblue]Sub[/color] Dog_Cat_Rat()
    [color=darkblue]Dim[/color] i [color=darkblue]As[/color] [color=darkblue]Long[/color], lFirst [color=darkblue]As[/color] [color=darkblue]Long[/color], strDCR [color=darkblue]As[/color] [color=darkblue]String[/color], strName [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] arr [color=darkblue]As[/color] [color=darkblue]Variant[/color], c [color=darkblue]As[/color] [color=darkblue]Variant[/color], v [color=darkblue]As[/color] [color=darkblue]Variant[/color]
    arr = Array("Dog", "Cat", "Rat")
    [color=darkblue]With[/color] Range("A1").CurrentRegion.Offset(1)
        v = .Value
        [color=darkblue]For[/color] i = 1 [color=darkblue]To[/color] [color=darkblue]UBound[/color](v, 1)
            [color=darkblue]If[/color] v(i, 1) <> strName [color=darkblue]Then[/color]
                [color=darkblue]If[/color] strDCR <> "" [color=darkblue]Then[/color] v(lFirst, 2) = Mid(strDCR, 3)
                strName = v(i, 1)
                lFirst = 0
                strDCR = ""
            [color=darkblue]End[/color] [color=darkblue]If[/color]
            [color=darkblue]For[/color] [color=darkblue]Each[/color] c [color=darkblue]In[/color] arr
                [color=darkblue]If[/color] LCase(v(i, 2)) [color=darkblue]Like[/color] "*" & LCase(c) & "*" [color=darkblue]Then[/color]
                    [color=darkblue]If[/color] lFirst = 0 [color=darkblue]Then[/color] lFirst = i
                    strDCR = strDCR & ", " & v(i, 2)
                    v(i, 2) = ""
                    [color=darkblue]Exit[/color] [color=darkblue]For[/color]
                [color=darkblue]End[/color] [color=darkblue]If[/color]
            [color=darkblue]Next[/color] c
        [color=darkblue]Next[/color] i
        .Value = v
    [color=darkblue]End[/color] [color=darkblue]With[/color]
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Solution

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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