+ Reply to Thread
Results 1 to 4 of 4

Sorting text within a single cell

  1. #1
    Registered User
    Join Date
    05-10-2021
    Location
    London
    MS-Off Ver
    10
    Posts
    9

    Sorting text within a single cell

    Hi,

    Someone ten years ago posted this VBA code which created a function which sorts text in an excel cell. Problem with the code is that it removes the spaces if any of the text is made up of more than one word, i.e Sourcing & Admin is changed to be Sourcing&Admin. Any ideas on how to change the code to leave the spaces in.

    This is the VBA code:
    Please Login or Register  to view this content.
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #2 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer

    This is the formula in Excel:

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Many thanks
    Last edited by 6StringJazzer; 06-26-2022 at 08:18 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,696

    Re: Sorting text within a single cell

    Just delete (or comment out) the first line of code. The first thing this function does is delete all the spaces. I don't even know why that would be there.

    Please Login or Register  to view this content.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    05-10-2021
    Location
    London
    MS-Off Ver
    10
    Posts
    9

    Re: Sorting text within a single cell

    Thanks for the quick reply, that didn't work (it needs cell to sort string to have value as used later) but it put me on the right track as instead of replacing spaces with nothing I've changed it to replace comma spaces with commas so now only removes the spaces between the different options.

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    I have a follow up question though if that's allowed. Can I get the above code to work as a macro instead of needing an excel formula to use it. I'm tagging this on the back of a private worksheet change code which allows data validation drop downs to select multiple entries in a single cell (hence the need to have it sort)
    Last edited by paularmstrong; 06-27-2022 at 08:44 AM.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,696

    Re: Sorting text within a single cell

    Please Login or Register  to view this content.
    You can do that with any function. For example this sorts the text in cell A1:

    Please Login or Register  to view this content.
    Here is a correction to my correction which I've actually tested. The error I made was the code used an intermediate variable and I did not notice. Maybe the change you made works better for your specific purposes, but my change does specifically what was asked in the original post. Take your pick.

    Please Login or Register  to view this content.

+ 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: 6
    Last Post: 11-19-2018, 09:46 AM
  2. Sorting a comma list inside a single cell
    By hintysen in forum Excel General
    Replies: 2
    Last Post: 06-15-2018, 10:29 AM
  3. [SOLVED] Sorting through text in single cell
    By Wikster7 in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 01-19-2016, 11:17 AM
  4. Sorting through text in single cell
    By Wikster7 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-15-2016, 05:15 PM
  5. Replies: 7
    Last Post: 01-24-2013, 06:55 PM
  6. Sorting within a single cell in Excel
    By JARF in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-03-2011, 07:36 PM
  7. Sorting within a single cell
    By KaruiKage in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 05-04-2009, 07:29 PM

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