Solved

Take a range and display all values in one cell as comma diliminated

Posted on 2016-10-04
12
28 Views
Last Modified: 2016-11-11
Hi,

I need help with a function that can take a range say A2:E5 and convert all values into one long string and place result into A1.

Then the reverse of this that takes the result from A1 and places into the defined range.  The values could be either text or integer.

Thank you
0
Comment
Question by:StormFusion
12 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 41828384
How should the values be arranged?

Should it be across then down or down then across?
0
 

Author Comment

by:StormFusion
ID: 41828392
Hi,

Across then down.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 41828411
You won't be able to use functions to do both the concatenation (values into A1) and extraction (values out of A1) because of circular referencing.

If the source range is known - A2:E5 in your example - then one could create a long formula that combines all the values like so:
=A2&","&B2&","&C2&","&D2&","&E2&","&A3&","&B3&","&C3&","&D3&","&E3&","&A4&","&B4&","&C4&","&D4&","&E4&","&A5&","&B5&","&C5&","&D5&","&E5

Admittedly, that's not very elegant.  

If your destination range was similarly-sized, you could insert formulas in the cells to retrieve the specific value, but they, too, would be pretty tedious.

Your best scenario would be to either have a macro or a user-defined function (UDF) that will handle these.  However, they involve VBA and you didn't specify whether that was an option.

-Glenn
0
 
LVL 28

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 500 total points
ID: 41828427
You may try a user defined function to get the desired output.

How to implement the Function:

  • Open your workbook and press Alt+F11 to open VB Editor.
  • On VB Editor --> Insert --> Module and paste the code given below into the opened code window.
  • Close VB Editor and save your workbook as Macro-Enabled Workbook.

Function RangeToString(rng As Range)
Dim cell As Range
Dim str As String
For Each cell In rng
   If str = "" Then
      str = cell.Value
   Else
      str = str & ", " & cell.Value
   End If
Next cell
RangeToString = str
End Function

Open in new window

Now try this...
In A1
=RangeToString(A2:E5)

Open in new window

1
 

Author Comment

by:StormFusion
ID: 41828440
Hi Subodh Tiwari (Neeraj) that works exactly as needed, but how do I now reverse this so it puts values from the function string back into the range defined,

So I use Range ToString (A1:A5) which works, and then another function which 0,0,0,0 for example is passed to and it places all the individual values back into the cells.

Cheers
0
 
LVL 33

Expert Comment

by:Norie
ID: 41828508
Here's an slightly different function which allows you to specify the delimiter and whether to go across and down or down and across.
Function RangeToDelimList(rng As Range, Optional Delim = ",", Optional AcrossDown = True)
Dim I As Long
Dim R As Long
Dim tmp As String
Dim arr()


    For I = 1 To rng.Rows.Count
        If DownAcross Then
            tmp = Join(Application.Transpose(Application.Index(Application.Transpose(rng), , I)), Delim)
        Else
            tmp = Join(Application.Transpose(Application.Index(rng, , I)), Delim)
        End If

        ReDim Preserve arr(R)

        arr(R) = tmp
        R = R + 1
    Next I

    RangeToDelimList = Join(arr, Delim)

End Function

Open in new window


It defaults to across and down with the delimiter being a comma.

As for a 'reverse' function, how would the range be determined?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:StormFusion
ID: 41828667
The range would be past to it similar to function
0
 
LVL 33

Expert Comment

by:Norie
ID: 41828729
But how would the range be determined?

Would it be passed to the function?
0
 

Author Comment

by:StormFusion
ID: 41828733
Ok, hows about when the first function is run it stores the RANGE in the first DIM slot, then when the comma deliminated code is passed to a new function it reads the first DIM slot for the range and it has exact size and range to be posted back?

SO.... A2:D5,TEST,DATA,DUMMY,VAR,001 etc

Then when other function called it nows exactly where it all goes back but obviously doesn't paste the RANGE value from the beginning of the string.

That be great :)
0
 
LVL 33

Expert Comment

by:Norie
ID: 41828755
So the first function would return the range address followed by the list of values from the range?
0
 

Author Comment

by:StormFusion
ID: 41828770
The first function would produce a comma deliminated list of data with the first of these values being the range it was taken from.

Then when this data string is past to another function it takes the first data from the string and knows this is the range the data needs to go into.
0
 
LVL 14

Expert Comment

by:frankhelk
ID: 41883537
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Subodh Tiwari (Neeraj) (https:#a41828427)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

frankhelk
Experts-Exchange Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now