Solved

Merge contents of selected cells in one cell

Posted on 2012-03-20
2
269 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

751 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