VBA - Concatenate - Dynamic Range - Row by Row

MKader

New Member
Joined
May 15, 2015
Messages
19
Hi All -

Need your help -

Was able to concatenate selected cells and value is placed in a designated cell -

How can I repeat the same process for all rows till the end of data in column -

Code:
Private Sub Trial_1_Click()
Dim CC As Range
Dim i As Integer
Dim TT As String
i = 0
TT = ""
For Each CC In Selection
If i = 0 Then
i = 1
End If
TT = TT & CC.Value & ""
Next
TT = Left(TT, Len(TT))
Range("P2").Value = TT
End Sub


Thanks,
MK
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You didn't specifically state where to start the concatenation from. I've assumed the Active Cell.

Rich (BB code):
Private Sub Trial_1_Click()
  Dim lr As Long
  
  lr = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
  Select Case lr - ActiveCell.Row
    Case 0: Range("P2").Value = ActiveCell.Value
    Case Is < 0: Range("P2").ClearContents
    Case Else: Range("P2").Value = Join(Application.Transpose(Range(ActiveCell, Cells(lr, ActiveCell.Column))), "")
  End Select
End Sub
 
Upvote 0
Hi Peter -

Thanks for your reply -

The selection is basically by highlighting the cells in the Row then click the button which result in P2 the concatenated value of the selected cells -

Thanks,
MK
 
Upvote 0
The selection is basically by highlighting the cells in the Row then click the button which result in P2 the concatenated value of the selected cells -
Isn't that what you code already did?
And aren't you looking for something different now?
Did you try my code?
 
Last edited:
Upvote 0
Hi -

I did try the code - it didn't work -

Yea - my code does that by concatenating the highlighted cells into the equivalent P2 - what am trying to do is to replicate this automatically on the rest of the sheet based on the initial selection as if am fixating a selection and applying to the rest -

Thanks,
MK
 
Upvote 0
Remember that we have not seen your sheet, nor know exactly what you are trying to do. All we have is what you tell us, so try to be clear & specific.

You initially said you wanted to "repeat the same process for all rows till the end of data in column". That's what I tried to give - concatenate everything from the first selected cell down the column until there was no more data. For example, in the sheet below, I had just cell K8 selected & ran the code. The results were put into P2.

Excel Workbook
KLMNOP
1
2aghijklm
3b
4c
5d
6e
7f
8g
9h
10i
11j
12k
13l
14m
15
Concat




Now you say you want to "replicate this automatically on the rest of the sheet". Does that mean the same as the previous statement?


Can you give some specific examples of where you have data, what you (might) have selected, what the code should do and where it should put the results?
Perhaps you could even show us a small set of dummy data and expected results? My signature block below has a link with info about how you can do that (example above).


When suggested code doesn't work, try to give feedback that might help us work out what to change. For example..
- did the code produce an error?
- did it concatenate the wrong data?
- did if concatenate the right data but put it in the wrong place?
- did it do nothing?
- did it do something else?
 
Upvote 0
Hi Peter -

Apologies if I am not that clear -

- did the code produce an error?
No
- did it concatenate the wrong data?
No
- did if concatenate the right data but put it in the wrong place?
No
- did it do nothing?
Yes
- did it do something else?
No


Maybe am not able to showcase what am trying to do -

When you select 2 & 3 then click the button - it result in 23
I am trying to have the rest apply the same - meaning the next cell would be 67, then 1011 and finally 1415

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD="align: center"]Data 1[/TD]
[TD="align: center"]Data 2[/TD]
[TD="align: center"]Data 3[/TD]
[TD="align: center"]Data 4[/TD]
[TD="align: center"]Result[/TD]
[/TR]
[TR]
[TD="align: center"]1[/TD]
[TD="align: center"]2[/TD]
[TD="align: center"]3[/TD]
[TD="align: center"]4[/TD]
[TD="align: center"]23[/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD="align: center"]6[/TD]
[TD="align: center"]7[/TD]
[TD="align: center"]8[/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]9[/TD]
[TD="align: center"]10[/TD]
[TD="align: center"]11[/TD]
[TD="align: center"]12[/TD]
[TD="align: center"][/TD]
[/TR]
[TR]
[TD="align: center"]13[/TD]
[TD="align: center"]14[/TD]
[TD="align: center"]15[/TD]
[TD="align: center"]16[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


Thanks,
MK
 
Upvote 0
OK, that's considerably clearer thanks. Though with that data and selection, my previous code should have produced 261014 in cell P2. Although that isn't what you wanted, it isn't nothing. :)

I'm assuming that you will always only have a single row and at least 2 columns selected. Give this one a try.

Rich (BB code):
Private Sub Trial_1_Click()
  Dim lr As Long, r As Long
  
  Application.ScreenUpdating = False
  lr = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
  For r = 0 To lr - ActiveCell.Row
    Range("P" & Selection.Row).Offset(r).Value = Join(Application.Index(Selection.Offset(r).Value, 1, 0), "")
  Next r
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi -

It did work - yet manually - meaning i have to select in each row the cells then click the button - :D

Thanks,
MK
 
Upvote 0

Forum statistics

Threads
1,225,611
Messages
6,185,996
Members
453,334
Latest member
Prakash Jha

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