Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Merge contents of selected cells in one cell

Posted on 2012-03-20
2
Medium Priority
?
275 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:
Frank White earned 2000 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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

661 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