+ Reply to Thread
Results 1 to 5 of 5

Take cell value spit on delimiter SORT the ensuing array then Join on delimiter post back

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Red face Take cell value spit on delimiter SORT the ensuing array then Join on delimiter post back

    I want to get a cell value then spit on a delimiter then SORT the ensuing array then Join the sorted array on the same delimiter then post back the ensuing string to the same cell

    e.g I have the cell value
    AA|MM|CC|WW|BB|NN
    I want it to be
    AA|BB|CC|MM|NN
    I get type mismatch error here
     For i = LBound(ar) To UBound(ar)
    The sorted array is empty, it seems the sorting is deleting the array? ... I am not sure what is going on, I have tried various sorting functions and I get the same error for all of them

    Thanks

    Sub Test()
    Dim ar As Variant
    Dim strTemp As String
    
       ar = Split(Cells(2, "E").Value, "|")
       ar = SortAry(ar)
       
       For i = LBound(ar) To UBound(ar)
          MsgBox ar(i)
       Next
       
       strTemp = Join(ar, "|")
       
       Cells(2, "E").Value = strTemp
       
    End Sub
    
    Function SortAry(pvarArray As Variant) As Variant
        Dim i As Long
        Dim iMin As Long
        Dim iMax As Long
        Dim varSwap As Variant
        Dim blnSwapped As Boolean
        
        iMin = LBound(pvarArray)
        iMax = UBound(pvarArray) - 1
        Do
            blnSwapped = False
            For i = iMin To iMax
                If pvarArray(i) > pvarArray(i + 1) Then
                    varSwap = pvarArray(i)
                    pvarArray(i) = pvarArray(i + 1)
                    pvarArray(i + 1) = varSwap
                    blnSwapped = True
                End If
            Next
            iMax = iMax - 1
        Loop Until Not blnSwapped
    End Function
    Last edited by capson; 02-05-2016 at 03:43 PM.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,641

    Re: Take cell value spit on delimiter SORT the ensuing array then Join on delimiter post

    Insert one line
        Loop Until Not blnSwapped
        SortAry = pvarArray
    End Function

  3. #3
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: Take cell value spit on delimiter SORT the ensuing array then Join on delimiter post

    Yes that did it. Thank you

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Take cell value spit on delimiter SORT the ensuing array then Join on delimiter post

    use the infamous bubblesrt function

    Sub Test()
    Dim ar As Variant
    Dim strTemp As String
    Dim i As Integer
    
       ar = BubbleSrt(Split(Cells(2, "E").Value, "|"), True)
       
       For i = LBound(ar) To UBound(ar)
          MsgBox ar(i)
       Next
       
       strTemp = Join(ar, "|")
       
       Cells(2, "E").Value = strTemp
       
    End Sub
    
    Public Function BubbleSrt(ArrayIn, Ascending As Boolean)
    Dim SrtTemp As Variant
    Dim i As Long
    Dim j As Long
    If Ascending = True Then
      For i = LBound(ArrayIn) To UBound(ArrayIn)
        For j = i + 1 To UBound(ArrayIn)
          If ArrayIn(i) > ArrayIn(j) Then
            SrtTemp = ArrayIn(j)
            ArrayIn(j) = ArrayIn(i)
            ArrayIn(i) = SrtTemp
          End If
        Next j
      Next i
    Else
      For i = LBound(ArrayIn) To UBound(ArrayIn)
        For j = i + 1 To UBound(ArrayIn)
          If ArrayIn(i) < ArrayIn(j) Then
            SrtTemp = ArrayIn(j)
            ArrayIn(j) = ArrayIn(i)
            ArrayIn(i) = SrtTemp
          End If
        Next j
       Next i
    End If
    BubbleSrt = ArrayIn
    End Function
    If you are happy with my response please click the * in the lower left of my post.

  5. #5
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: Take cell value spit on delimiter SORT the ensuing array then Join on delimiter post

    Thanks stnkynts that cleans things up

+ 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] How do I create a macro to import and sort a txt file with more than one delimiter?
    By JonnyEnglish in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-29-2016, 03:57 AM
  2. FORMULA: Find all matches within array, delimiter between duplicate sets
    By cslicer in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 10-16-2014, 10:59 AM
  3. Text to columns VBA code to change delimiter if the cell value is in array
    By dopple in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-26-2008, 09:25 AM
  4. Split Cell by Delimiter, Move to New Row...
    By jpfulton in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-16-2008, 03:43 AM
  5. Split Cell by Delimiter, Move to New Row...
    By jpfulton in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 09-08-2008, 03:53 PM
  6. semicolon delimiter in a cell
    By coco in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-02-2005, 06:05 PM
  7. Splitting a Cell (no useable delimiter).....
    By Puggy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-17-2005, 11:31 AM

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