#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Merge Cells

Hi...There have been many posts about merging cells, but I haven't seen this
issue addressed.

I want to take several cells in a column and merge them into one cell
separated by return characters. Example:

Input cells:
- - - - - -
| Bob | A1
- - - - - -
| Joe | A2
- - - - - -
| Ken | A3
- - - - - -

Desired result after merge:
- - - - - -
| Bob |
| Joe | A1
| Ken |
- - - - - -
| <blank | A2
- - - - - -
| <blank | A3
- - - - - -

I can create the result by manually entering returns via
Option-Command-Return on my Mac and (IIRC) Ctrl-Return on my PC.

I wrote the following macro to automate the merge. Unfortunately, the
inserted CRs are not recognized in the merged cell (i.e., I get BobJoeKen in
A1):

Sub MergeCells()
Dim rng As Range
Dim cell As Range
Dim OutStr As String

Set rng = Intersect(Selection, ActiveSheet.UsedRange)

If rng Is Nothing Then
MsgBox "Select a range to be merged"
GoTo done
End If

For Each cell In rng
OutStr = OutStr & cell.Text & Chr(13)
Next cell

OutStr = Left(OutStr, Len(OutStr) - 1) ' Strip off final CR
rng.ClearContents ' Clear original selected range
rng.Cells(1).Value = OutStr ' Put new string into top cell
done:
End Sub

Yet, if I manually enter A (CR) B (CR) C into a cell and run the following
macro, I see that the three lines are indeed separated by two Chr(13). What
am I missing?

Sub GetSeparatorChar()
' Select one cell with "A," "B," and "C" in it separated by
' carriage returns (CR)
Dim c As String
Dim i As Integer

For i = 1 To Len(Selection.Text)
Debug.Print Asc(Mid(Selection.Text, i, 1))
Next i

End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Merge Cells

I don't think so.

"Steve Drenker" wrote:

Hi...There have been many posts about merging cells, but I haven't seen this
issue addressed.

I want to take several cells in a column and merge them into one cell
separated by return characters. Example:

Input cells:
- - - - - -
| Bob | A1
- - - - - -
| Joe | A2
- - - - - -
| Ken | A3
- - - - - -

Desired result after merge:
- - - - - -
| Bob |
| Joe | A1
| Ken |
- - - - - -
| <blank | A2
- - - - - -
| <blank | A3
- - - - - -

I can create the result by manually entering returns via
Option-Command-Return on my Mac and (IIRC) Ctrl-Return on my PC.

I wrote the following macro to automate the merge. Unfortunately, the
inserted CRs are not recognized in the merged cell (i.e., I get BobJoeKen in
A1):

Sub MergeCells()
Dim rng As Range
Dim cell As Range
Dim OutStr As String

Set rng = Intersect(Selection, ActiveSheet.UsedRange)

If rng Is Nothing Then
MsgBox "Select a range to be merged"
GoTo done
End If

For Each cell In rng
OutStr = OutStr & cell.Text & Chr(13)
Next cell

OutStr = Left(OutStr, Len(OutStr) - 1) ' Strip off final CR
rng.ClearContents ' Clear original selected range
rng.Cells(1).Value = OutStr ' Put new string into top cell
done:
End Sub

Yet, if I manually enter A (CR) B (CR) C into a cell and run the following
macro, I see that the three lines are indeed separated by two Chr(13). What
am I missing?

Sub GetSeparatorChar()
' Select one cell with "A," "B," and "C" in it separated by
' carriage returns (CR)
Dim c As String
Dim i As Integer

For i = 1 To Len(Selection.Text)
Debug.Print Asc(Mid(Selection.Text, i, 1))
Next i

End Sub


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
Automatically merge mulitiple cells to one cells Edward Wang Excel Worksheet Functions 5 September 15th 09 07:56 PM
how do I merge cells into one then delete the original cells? LLR Excel Worksheet Functions 2 March 7th 08 10:59 PM
How can I have formatting options like merge cells ,Bold,active for the unlocked cells of the protected worksheet.Is it possible in excel? divya Excel Programming 2 July 20th 06 02:04 PM
How do I merge cells in Excel, like just 2 cells to make one big . chattacat Excel Discussion (Misc queries) 2 January 19th 05 04:25 PM
fill cells, merge cells porkie[_4_] Excel Programming 1 September 23rd 04 04:21 AM


All times are GMT +1. The time now is 11:39 PM.

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

About Us

"It's about Microsoft Excel"