Loop down rows on multiple selected cells

MadeleineB

New Member
Joined
Sep 23, 2008
Messages
18
I have a sub that will join the text together from multiple columns into one cell but it only works for one row. I would like it to work through the rows selected. How do I do this?

Sub JoinText()

myCol = Selection.Columns.Count
For i = 1 To myCol
ActiveCell = ActiveCell.Offset(0, 0) & ActiveCell.Offset(0, i)
ActiveCell.Offset(0, i) = ""
Next i

End Sub
 
Last edited:

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.
Hello Madeleine

Do you mean if you select A1:G100 then the joined text will be placed in A1 for row 1, A2 for row 2, A3 for row 3 etc, or do you want the text from all the cells (A1:G100) to be joined together and placed in A1?
 
Upvote 0
If (A1:G100) I would like the result to be:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
A1 = cells A1 to G1<o:p></o:p>
A2 = cells A2 to G2 <o:p></o:p>
<o:p></o:p>
e.t.c.
 
Upvote 0
Try this:

Code:
Sub Test()
Dim arr As Variant, i As Long, j As Long, temp, arrOut
arr = Selection.Value
ReDim arrOut(1 to UBound(arr,1),1 To 1)
For i = 1 to UBound(arr,1)
For j = 1 to UBound(arr,2)
temp = temp & arr(i,j)
Next j
arrOut(i,1) = Temp
temp = ""
Next i
Selection.ClearContents
Selection(1).Resize(Ubound(arrOut,1)).Value = arrOut
End Sub
 
Upvote 0
Yep that does it thanks Richard.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
Just to tweak it, is there a way to put a space between each cells text?<o:p></o:p>
 
Upvote 0
Sure:

Code:
Sub Test()
Dim arr As Variant, i As Long, j As Long, temp, arrOut
arr = Selection.Value
ReDim arrOut(1 to UBound(arr,1),1 To 1)
For i = 1 to UBound(arr,1)
For j = 1 to UBound(arr,2)
temp = temp & " " & arr(i,j)
Next j
arrOut(i,1) = Mid(temp,2)
temp = ""
Next i
Selection.ClearContents
Selection(1).Resize(Ubound(arrOut,1)).Value = arrOut
End Sub


Aha! You got there before I did ;)
 
Upvote 0
Dam, I've just be told that I need to combine columns and not rows.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
Help again please.<o:p></o:p>
 
Upvote 0
Assuming if A1:G100 selected then you want the results in A1:G1 then:

Code:
Sub Test()
Dim arr As Variant, i As Long, j As Long, temp, arrOut
arr = Selection.Value
ReDim arrOut(1 To 1,1 to UBound(arr,2))
For i = 1 to UBound(arr,2)
For j = 1 to UBound(arr,1)
temp = temp & " " & arr(j,i)
Next j
arrOut(1,i) = Mid(temp,2)
temp = ""
Next i
Selection.ClearContents
Selection(1).Resize(,Ubound(arrOut,2)).Value = arrOut
End Sub
 
Upvote 0
Great it works a treat.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
Thanks for all your help. It has saved me doing a very tedious task manually. :)<o:p></o:p>
 
Upvote 0

Forum statistics

Threads
1,215,666
Messages
6,126,106
Members
449,292
Latest member
Mario BR

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