need a macro to replace and sort within a cell

kimsa

New Member
Joined
Feb 26, 2016
Messages
15
I have words in a cell separated by a semicolon and need a macro to replace the ";" and put the words in a alpha list within the cell

have this:
black;blue;green;red;purple

want this:

black
blue
green
purple
red
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Re: need a macro to repace and sort within a cell

How about
Code:
Sub SplitSort()
   Dim Lst As Object
   Dim Elm As Variant
   Dim cl As Range
   
   Set Lst = CreateObject("system.collections.arraylist")
   For Each cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      For Each Elm In Split(cl, ";")
         Lst.add Elm
      Next Elm
      Lst.Sort
      cl = Join(Lst.toarray, Chr(10))
      Lst.Clear
   Next cl
End Sub
 
Upvote 0
Re: need a macro to repace and sort within a cell

thank you this worked well how do I edit it because want it to run for three columns and to apply it to columns C2:C52, D2:D52, E2:E52. Also within a cell I will have 5-10 words and all will be in alpha order except the last word is not and not sure why?

How about
Code:
Sub SplitSort()
   Dim Lst As Object
   Dim Elm As Variant
   Dim cl As Range
   
   Set Lst = CreateObject("system.collections.arraylist")
   For Each cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      For Each Elm In Split(cl, ";")
         Lst.add Elm
      Next Elm
      Lst.Sort
      cl = Join(Lst.toarray, Chr(10))
      Lst.Clear
   Next cl
End Sub
 
Upvote 0
Re: need a macro to repace and sort within a cell

This will work on the range specified
Code:
Sub SplitSort()
   Dim Lst As Object
   Dim Elm As Variant
   Dim cl As Range
   
   Set Lst = CreateObject("system.collections.arraylist")
   For Each cl In Range("C2:E52")
      For Each Elm In Split(cl, ";")
         Lst.add Elm
      Next Elm
      Lst.Sort
      cl = Join(Lst.toarray, Chr(10))
      Lst.Clear
   Next cl
End Sub
Can you show some examples of the text that is not getting sorted correctly.
 
Upvote 0
Re: need a macro to repace and sort within a cell

this is some text that did not get sorted:

economic burden
epidemilogy
survival
quality of life

or:

disease
genetics
lung cancers
sclc
mutated genes
 
Upvote 0
Re: need a macro to repace and sort within a cell

This is what I get


Excel 2013/2016
ABC
1BeforeAfter
2economic burden;epidemilogy;survival;quality of lifeeconomic burden epidemilogy quality of life survival
3disease;genetics;lung cancers;sclc;mutated genesdisease genetics lung cancers mutated genes sclc
Sheet2


What are your "before" values?
 
Upvote 0
Re: need a macro to repace and sort within a cell

i figured the problem is that I have a space after the semicolon. Can I have a step first to remove the spaces after the semicolon? Can you please give me the code for that so my before looks like this:

economic burden; epidemilogy; survival; quality of life


sorry I am a newbie at these macros, I really appreciate your help, It has save me great time!
 
Last edited:
Upvote 0
Re: need a macro to repace and sort within a cell

Simply add a space after the ; on this line as shown
Code:
For Each Elm In Split(cl, "; ")
 
Upvote 0
Re: need a macro to repace and sort within a cell

I have words in a cell cell and would like to have the brackets and single quotes removed and put in alpha order. I have this in a single cell: ['red', 'blue', 'green', 'yellow']? I wnat this in a single cell:

blue
green
red
yellow
 
Upvote 0
Re: need a macro to repace and sort within a cell

As this is now a different question, please start a new thread.
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top