+ Reply to Thread
Results 1 to 6 of 6

How do i keep the spaces after using the: =SortWithinCell(A1,",",TRUE) vba? please.

Hybrid View

Adz847 How do i keep the spaces... 09-02-2021, 09:42 AM
dangelor Re: How do i keep the spaces... 09-02-2021, 10:19 AM
Adz847 Re: How do i keep the spaces... 09-02-2021, 10:21 AM
dangelor Re: How do i keep the spaces... 09-02-2021, 07:11 PM
Adz847 Re: How do i keep the spaces... 09-03-2021, 02:46 AM
dangelor Re: How do i keep the spaces... 09-03-2021, 07:17 AM
  1. #1
    Registered User
    Join Date
    09-02-2021
    Location
    Southampton, England
    MS-Off Ver
    2016
    Posts
    3

    How do i keep the spaces after using the: =SortWithinCell(A1,",",TRUE) vba? please.

    Hi,

    I have used this vba to sort a list of names in a single cell alphabetically, however, when I do it removes all of the spaces... e.g

    Darren H becomes darrenH...

    Is there a way to keep the spaces please?

    Any help greatly appreciated...

  2. #2
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,318

    Re: How do i keep the spaces after using the: =SortWithinCell(A1,",",TRUE) vba? please.

    Can you post the code for that function?

  3. #3
    Registered User
    Join Date
    09-02-2021
    Location
    Southampton, England
    MS-Off Ver
    2016
    Posts
    3

    Re: How do i keep the spaces after using the: =SortWithinCell(A1,",",TRUE) vba? please.

    Sure,

    Function SortWithinCell(CelltoSort As Range, DelimitingCharacter As String, IncludeSpaces As Boolean) As String
    CelltoSortString = WorksheetFunction.Substitute(CelltoSort.Value, " ", "")
    MyArray = Split(CelltoSortString, DelimitingCharacter)
    For N = 0 To UBound(MyArray)
    For M = 1 To UBound(MyArray)

    If MyArray(M) < MyArray(M - 1) Then
    TempValue = MyArray(M)
    MyArray(M) = MyArray(M - 1)
    MyArray(M - 1) = TempValue
    End If

    Next M
    Next N
    For N = 0 To UBound(MyArray)
    SortWithinCell = SortWithinCell & MyArray(N) & DelimitingCharacter
    Next N
    SortWithinCell = Left(SortWithinCell, Len(SortWithinCell) - 1)
    If IncludeSpaces = True Then SortWithinCell = WorksheetFunction.Substitute(SortWithinCell, ",", ", ")
    End Function

    Then back into the spreadsheet: =SortWithinCell(A1,",",TRUE) if the cell was A1 obviously.

    Regards...

  4. #4
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,318

    Re: How do i keep the spaces after using the: =SortWithinCell(A1,",",TRUE) vba? please.

    Possibly...
    Function SortWithinCell(CelltoSort As Range, DelimitingCharacter As String, IncludeSpaces As Boolean) As String
        MyArray = Split(CelltoSort.Value, DelimitingCharacter)
        For N = 0 To UBound(MyArray)
            For M = 1 To UBound(MyArray)
                
                If MyArray(M) < MyArray(M - 1) Then
                    TempValue = MyArray(M)
                    MyArray(M) = MyArray(M - 1)
                    MyArray(M - 1) = TempValue
                End If
                
            Next M
        Next N
        For N = 0 To UBound(MyArray)
            SortWithinCell = SortWithinCell & MyArray(N) & DelimitingCharacter
        Next N
        SortWithinCell = Left(SortWithinCell, Len(SortWithinCell) - 1)
        If IncludeSpaces = True Then SortWithinCell = WorksheetFunction.Substitute(SortWithinCell, ",", ", ")
    End Function

  5. #5
    Registered User
    Join Date
    09-02-2021
    Location
    Southampton, England
    MS-Off Ver
    2016
    Posts
    3

    Re: How do i keep the spaces after using the: =SortWithinCell(A1,",",TRUE) vba? please.

    Hi,

    Thank you for this, however it is bringing up an error in the string:

    Attachment 746342

    Kind regards,

    Adz

  6. #6
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    MS365 V.2406
    Posts
    2,318

    Re: How do i keep the spaces after using the: =SortWithinCell(A1,",",TRUE) vba? please.

    Your attachment is not readable. Can you post the error and the highlighted line of code?

+ 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. Replies: 1
    Last Post: 05-24-2021, 11:24 AM
  2. Replies: 1
    Last Post: 05-24-2021, 11:24 AM
  3. Replies: 6
    Last Post: 03-07-2014, 10:34 PM
  4. compare two excel sheets using macros and display the result in "true" or "false"
    By gayunana01 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-09-2012, 07:21 AM
  5. Replies: 5
    Last Post: 10-12-2010, 06:46 AM
  6. Replies: 7
    Last Post: 05-13-2006, 05:02 PM
  7. [SOLVED] IF(VLOOKUP("MYDATA", MYNAME, 4) = 0, "TRUE", "FALSE")
    By Souris in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-17-2005, 01:05 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