+ Reply to Thread
Results 1 to 2 of 2

Function for Comma-Separated Values within Cell

Hybrid View

  1. #1
    Registered User
    Join Date
    07-12-2014
    Location
    Chicago
    MS-Off Ver
    2013
    Posts
    67

    Function for Comma-Separated Values within Cell

    I already have a function to take a comma-separated string within a cell and change the values into dashes (I'll attach the .bas file) Here's an example of how it already works:
    A B C
    1 Comma-Separated Cells "Numdash" Function Result
    2 1,2,3,4,5 =NUMDASH(A2) 1-5
    3 1,2,3,5 =NUMDASH(A3) 1-3,5
    4 1,2,3,5,7,8,9,10,11,12 =NUMDASH(A4) 1-3,5,7-12

    Now I need a way to reverse-engineer it so it takes the dashed values and shows all the numbers between them, like this:

    A B C
    1 Dash-Separated Cells "Numcomma" Function Result
    2 1-5 =NUMCOMMA(A2) 1,2,3,4,5
    3 1-3,5 =NUMCOMMA(A3) 1,2,3,5
    4 1-3,5,7-12 =NUMCOMMA(A4) 1,2,3,5,7,8,9,10,11,12

    Here's a sample of how the macro that I have works;
    NUMDASH.xlsm
    Option Explicit
    Function NUMDASH(txt As String) As String
    'UDF to summarize sequential numbers by range using dash (-)
    'e.g. 1,2,3,6,7,8 becomes 1-3,6-8 in result
        Dim e, i As Long
        If txt Like "*,*" Then
            With CreateObject("System.Collections.ArrayList")
                For Each e In Split(txt, ",")
                    If Val(e) <> 0 Then
                        If Not .Contains(Val(e)) Then .Add Val(e)
                    End If
                Next
                If .Count > 1 Then
                    .Sort: NUMDASH = .Item(0) & "-"
                    For i = 1 To .Count - 1
                        If .Item(i) - .Item(i - 1) > 1 Then
                            NUMDASH = IIf(NUMDASH Like "*-", Left$(NUMDASH, Len(NUMDASH) - 1), NUMDASH) _
                                    & "," & .Item(i) & "-"
                        Else
                            NUMDASH = Left$(NUMDASH, InStrRev(NUMDASH, "-") - 1) & "-" & .Item(i)
                        End If
                    Next
                    NUMDASH = IIf(NUMDASH Like "*-", Left$(NUMDASH, Len(NUMDASH) - 1), NUMDASH)
                Else
                    NUMDASH = .Item(0)
                End If
            End With
        Else
            NUMDASH = txt
        End If
    End Function
    Last edited by SoothSailor; 11-07-2014 at 04:30 PM. Reason: EDIT: Attached file

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Function for Comma-Separated Values within Cell

    Public Function NumComma(ByRef cell As Range) As String
    Dim tmp As String
    Dim ary As Variant
    Dim pos As Long
    Dim i As Long, ii As Long
    
        ary = Split(cell.Value, ",")
        For i = LBound(ary) To UBound(ary)
        
            pos = InStr(ary(i), "-")
            If pos > 0 Then
            
                For ii = Left$(ary(i), pos - 1) To Mid$(ary(i), pos + 1, 99)
                
                    tmp = tmp & ii & ","
                Next ii
            Else
                
                tmp = tmp & ary(i) & ","
            End If
        Next i
        
        NumComma = Left$(tmp, Len(tmp) - 1)
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Compare comma separated values in a cell to a list
    By SMB in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-03-2019, 01:10 AM
  2. Replies: 7
    Last Post: 07-04-2014, 11:52 AM
  3. [SOLVED] Extracting information from a comma separated list of values in one cell
    By cardiff in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-11-2012, 09:17 PM
  4. Replies: 3
    Last Post: 01-13-2012, 08:20 AM
  5. Resolved >>> Comma separated values in a cell
    By usr789 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-25-2007, 08:36 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1