Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
TJM
 
Posts: n/a
Default Macro Help: Concatenate Populated Cells in Column A


Hello all -

Here is what I am wanting to do: I have a spreadsheet that has X number
of cells populated in column A. I want to write a macro that will do the
following: concatenate all populated cells in column A into one cell,
separated by a comma (with no space).

Column A
A1
A2
A3

Becomes
A1,A2,A3

The cells in column A may vary depending on a given spreadsheet. I want
to be able to use the macro on any spreadsheet without having to tweak
the cell range manually.

Thanks!

Tom


--
TJM
------------------------------------------------------------------------
TJM's Profile: http://www.excelforum.com/member.php...o&userid=12746
View this thread: http://www.excelforum.com/showthread...hreadid=378139

  #2   Report Post  
Jason Morin
 
Posts: n/a
Default

Try:

Sub MConcat()

Dim cell As Range
Dim rDestCell As Range
Dim nLastRow As Long
Dim strConcat As String
Const cDelim As String = ","

nLastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set rDestCell = ActiveSheet.[D1] '<-- Change cell

For Each cell In Range("A1:A" & nLastRow)
If Not IsEmpty(cell) Then
strConcat = strConcat & cell.Text & cDelim
End If
Next

rDestCell = Left(strConcat, Len(strConcat) - 1)

End Sub

---
HTH
Jason
Atlanta, GA

"TJM" wrote:


Hello all -

Here is what I am wanting to do: I have a spreadsheet that has X number
of cells populated in column A. I want to write a macro that will do the
following: concatenate all populated cells in column A into one cell,
separated by a comma (with no space).

Column A
A1
A2
A3

Becomes
A1,A2,A3

The cells in column A may vary depending on a given spreadsheet. I want
to be able to use the macro on any spreadsheet without having to tweak
the cell range manually.

Thanks!

Tom


--
TJM
------------------------------------------------------------------------
TJM's Profile: http://www.excelforum.com/member.php...o&userid=12746
View this thread: http://www.excelforum.com/showthread...hreadid=378139


  #3   Report Post  
Easy Solutions
 
Posts: n/a
Default

If the number of rows is always the same in column A, then you could use the
& funcionality in excel and simply us =A1&","&A2&","&a3...... however it will
not help if the result is variable in length.

"TJM" wrote:


Hello all -

Here is what I am wanting to do: I have a spreadsheet that has X number
of cells populated in column A. I want to write a macro that will do the
following: concatenate all populated cells in column A into one cell,
separated by a comma (with no space).

Column A
A1
A2
A3

Becomes
A1,A2,A3

The cells in column A may vary depending on a given spreadsheet. I want
to be able to use the macro on any spreadsheet without having to tweak
the cell range manually.

Thanks!

Tom


--
TJM
------------------------------------------------------------------------
TJM's Profile: http://www.excelforum.com/member.php...o&userid=12746
View this thread: http://www.excelforum.com/showthread...hreadid=378139


  #4   Report Post  
Jack Sons
 
Posts: n/a
Default

Jason,

What code is to be used for not concatenating all contigiously used cells in
column A, but for all cells in a selected range, even if one ore more cells
in that range are blank (empty)?

Jack Sons
The Netherlands

"Jason Morin" schreef in bericht
...
Try:

Sub MConcat()

Dim cell As Range
Dim rDestCell As Range
Dim nLastRow As Long
Dim strConcat As String
Const cDelim As String = ","

nLastRow = Cells(Rows.Count, "A").End(xlUp).Row
Set rDestCell = ActiveSheet.[D1] '<-- Change cell

For Each cell In Range("A1:A" & nLastRow)
If Not IsEmpty(cell) Then
strConcat = strConcat & cell.Text & cDelim
End If
Next

rDestCell = Left(strConcat, Len(strConcat) - 1)

End Sub

---
HTH
Jason
Atlanta, GA

"TJM" wrote:


Hello all -

Here is what I am wanting to do: I have a spreadsheet that has X number
of cells populated in column A. I want to write a macro that will do the
following: concatenate all populated cells in column A into one cell,
separated by a comma (with no space).

Column A
A1
A2
A3

Becomes
A1,A2,A3

The cells in column A may vary depending on a given spreadsheet. I want
to be able to use the macro on any spreadsheet without having to tweak
the cell range manually.

Thanks!

Tom


--
TJM
------------------------------------------------------------------------
TJM's Profile:
http://www.excelforum.com/member.php...o&userid=12746
View this thread:
http://www.excelforum.com/showthread...hreadid=378139




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy cell format to cell on another worksht and update automatical kevinm Excel Worksheet Functions 21 May 19th 05 11:07 AM
Count Position of Filtered TEXT cells in a column Sam via OfficeKB.com Excel Worksheet Functions 0 May 15th 05 08:14 PM
COUNT NON-BLANK CELLS WITH REFERENCE TO ANOTHER COLUMN carricka Excel Worksheet Functions 1 May 6th 05 04:50 PM
Macro to delete data in 'green' cells only Steve Excel Worksheet Functions 7 March 19th 05 01:40 PM
Auto Skipping and protected cells Dave Peterson Excel Discussion (Misc queries) 6 January 27th 05 11:35 PM


All times are GMT +1. The time now is 07:05 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"