VBA Loop Selected Rows to One Cell with Comma's Appended

gutermdp

New Member
Joined
Dec 10, 2013
Messages
10
Hey All,

I have a two part question, the first being just a generic request for information that will hopefully alleviate the need for posts like this, and the second being an actual issue I'm trying to conquer.

So Question 1, I'm terrible with loops, can someone recommend a number of resources you found were best to help you get over the hump and use them effectively? Web, Books, whatever the media is fine.

Question 2, I have a list of data in from A1:A10, each item represents a color in my mock example. I want to have B1 populate with what would essentially be a concatenated list of values from my selected cells in Column A and append a comma after each.

Example:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Teal[/TD]
[TD]Teal, Blue, Green, Red, Orange, Yellow, Purple, Maroon, Magenta, Black[/TD]
[/TR]
[TR]
[TD]Blue[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Green[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Red[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Orange[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Yellow[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Purple[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Maroon[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Magenta[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Black[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

I know how I can accomplish this with a number of formulas, but I'm trying to utilize loops/VBA/whatever would be most efficient to not have to keep creating custom formulas as we do this often at work to create strings for data searches.

Will look to execute with hotkey/button press, but I can handle that.

Thanks,
Dan.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Q(1) :- Practice !!
Q(2) try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG18Feb02
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant
Ray = Application.Transpose(Range(Range("A1"), Range("A" & Rows.Count).End(xlUp)))
[COLOR="Navy"]With[/COLOR] Range("B1")
    .Value = Join(Ray, ",")
    .WrapText = True
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Question 1 : Google

Question 2 : Using loops with comments

Code:
Sub Loop_Macro()
Dim Result As String
Dim I As Integer


'Assign First Value to Result
Result = Cells(1, 1).Value


'Loop from 2 to 10th row in column A and assign value to result value with previous result value
For I = 2 To 10
    Result = Result & "," & Cells(I, 1).Value
Next I


'Assign result to Cell B2
Range("B1").Value = Result


End Sub
 
Upvote 0
Question 1 : Google

Question 2 : Using loops with comments

Code:
Sub Loop_Macro()
Dim Result As String
Dim I As Integer


'Assign First Value to Result
Result = Cells(1, 1).Value


'Loop from 2 to 10th row in column A and assign value to result value with previous result value
For I = 2 To 10
    Result = Result & "," & Cells(I, 1).Value
Next I


'Assign result to Cell B2
Range("B1").Value = Result


End Sub


Thanks, this works great.

Is it possible to make this dynamic, so rather then being based off of a range of cells it would be done in conjunction with activesheet.selection?

I'd expect the values to print to offset(0,1).

Thanks!
 
Upvote 0
Thanks, this works great.

Is it possible to make this dynamic, so rather then being based off of a range of cells it would be done in conjunction with activesheet.selection?

I'd expect the values to print to offset(0,1).

Thanks!

Maybe something like this..

Code:
Sub Loop_Macro()
Dim Result As String
Dim I As Integer
[COLOR=#0000cd]Dim LastRow As Long[/COLOR]


[COLOR=#0000cd]'Find LastRow USED in column A[/COLOR]
[COLOR=#0000cd]LastRow = Range("A" & Rows.Count).End(xlUp).Row[/COLOR]


'Assign First Value to Result
Result = Cells(1, 1).Value




'Loop from 2 to last row row in column A and assign value to result value with previous result value
For I = 2 To [COLOR=#0000cd]LastRow[/COLOR]
    Result = Result & "," & Cells(I, 1).Value
Next I


'Assign result to Cell B2
Range("B1").Value = Result


'Note : In excel 2010 Maximum character allowed is 32767.


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,555
Members
452,928
Latest member
101blockchains

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