Solved

Merge contents of selected cells in one cell

Posted on 2012-03-20
2
264 Views
Last Modified: 2012-03-20
Cells_To_Merge      Result
17                                17,15,20
15      
20      
      
Dear Experts,       
      
I would like to select any number of cells in a column.      
The macro that is activated is to merge the contents of the selected cells in one cell, whereby the entries are to be separated by a comma.       
The merged content is to be filled into the cell that is adjacent to the first cell of the selection      
      
I have attached a sample file with detailed instructions and an example

Help is much appreciated. Thank you very much in advance.

Regards, Andreas      
Merging-Cells.xls
0
Comment
Question by:AndreasHermle
2 Comments
 
LVL 3

Accepted Solution

by:
DaFranker earned 500 total points
ID: 37743121
This code will make a string out of the text shown by the selected range and put the result with comma separators in the cell immediately to the right of the top-left cell in the selection.

Note that this will produce unwanted results if you select a range with more than one column and/or more than one area.

Option Explicit

Sub MergeMacro()
    Dim rMyRange As Range
    Dim rCurCell As Range
    Dim sResult As String
    Dim iCount As Integer
    Set rMyRange = ActiveWindow.RangeSelection
    iCount = 1

    For Each rCurCell In rMyRange
        If rCurCell.Text <> "" Then
            If iCount > 1 Then sResult = sResult & ","
            sResult = sResult & rCurCell.Text
            iCount = iCount + 1
        End If
    Next
    
    rMyRange.Offset(0, 1).Resize(1, 1).Value = sResult
    
End Sub

Open in new window


As it is, the macro must be run manually. I'm assuming you know how to create a VBA module and run this as a macro, but if you need help with that just say so.
0
 

Author Closing Comment

by:AndreasHermle
ID: 37743253
Hi DaFranker,

great job. Wonderful!  Prompt support and excise, sophisticated code.

This forum deserves its name.

Thank you very much. Regards,  Andreas
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question