Concatenating of dynamic cell range. HELP

nithishvinolin

New Member
Joined
May 31, 2013
Messages
5
Hi,

I need a help in developing a macro.
I have a example. I have 4 columns. first 3 columns ID, C1, C2 have the input data. Under a ID we have multiple number of rows. In this example we have 4 rows for ID1. But in real time it can be any number of rows under a single ID. When I click a button the values under ID 1 should get concatenated and stored under the Concatenate column.
I tried some macros but i cant get the logic behind finding the dynamically changing row counts under single ID. Any kind of help will be appreciated. Thanks in advance.


IDC1C2Concatenate
11212345678
34
56
78
291091011121314
1112
1314
3151615161718
1718

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

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG04Jun31
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Temp [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not Dn.Offset(, -1) = vbNullString [COLOR="Navy"]Then[/COLOR] [COLOR="Navy"]Set[/COLOR] Temp = Dn
    Temp.Offset(, 2) = Temp.Offset(, 2) & Dn & Dn.Offset(, 1)
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hi Mick,

Thanks a lot. It worked. You are a genius. I was kicking my brains for this.

One kind help. Can you explain the logic behind the vba? I am feeling tough to understand. I am very new to macro. It will be so much useful for me to proceed further.


Thanks,
Nithish
 
Upvote 0
This may help !!
Code:
[COLOR="Navy"]Sub[/COLOR] MG04Jun13
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Dim[/COLOR] Temp [COLOR="Navy"]As[/COLOR] Range
'[COLOR="Green"][B]set Rng = range of cells in column "B" that have data[/B][/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("B2"), Range("B" & Rows.Count).End(xlUp))


'[COLOR="Green"][B]Loop through Rng[/B][/COLOR]
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
   
   '[COLOR="Green"][B]If columns "A" Value is not "Blank" then set that Cell to range variable "Temp"[/B][/COLOR]
   '[COLOR="Green"][B]This range (Temp) will stay the same until the loop find another cell in[/B][/COLOR]
   '[COLOR="Green"][B]column "A" with a value.[/B][/COLOR]
    [COLOR="Navy"]If[/COLOR] Not Dn.Offset(, -1) = vbNullString [COLOR="Navy"]Then[/COLOR] [COLOR="Navy"]Set[/COLOR] Temp = Dn
    
    
    '[COLOR="Green"][B]Each value in the loop is Concatenated in "Temp" Range offset(,2)= Column "D"[/B][/COLOR]
    Temp.Offset(, 2) = Temp.Offset(, 2) & Dn & Dn.Offset(, 1)
[COLOR="Navy"]Next[/COLOR] Dn
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,662
Members
449,462
Latest member
Chislobog

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