{"id":12086,"date":"2011-11-14T18:39:02","date_gmt":"2011-11-14T18:39:02","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/?p=639"},"modified":"2018-01-04T22:54:30","modified_gmt":"2018-01-04T22:54:30","slug":"sorting-a-string-stored-in-a-cell-of-excel","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/","title":{"rendered":"Sorting a String stored in a cell of Excel"},"content":{"rendered":"

Dear Readers,<\/p>\n

By using Excel Macro, we can sort a String which is stored in a Cell in an Excel Sheet. Sorting will be done as a String. For example (Ascending): First all Numeric Values in ascending Order then Special Chars then Alphabets in ascending etc.
\n

\"Sorting\"<\/a>

Sorting<\/p><\/div>
\nFor this we need to copy and Paste below Sub Function<\/strong> in the Module of your Excel VBA. Now call this function from which ever sheet you want.<\/p>\n

\r\nSub Sorting()\r\n\r\nDim str_array(1000), temp, input_str As String\r\nDim flag As Boolean\r\nflag = False\r\n\r\ninput_str = Range(\"A1\").Value\r\nl = Len(input_str)\r\n\r\n'--- Store complete string in an array format letter by letter\r\n\r\nFor i = 1 To l\r\ntemp = Left(input_str, i)\r\nstr_array(i) = Right(temp, 1)\r\nNext\r\n\r\n'--- We need to Sort all the letters in ascending or Descending Order\r\n'--- For that i will add a Sheet in workbook and copy all the letters in one column and sort it by Excel Sort functionality\r\n\r\nFor Each Sheet In Sheets\r\nIf Sheet.Name = \"config\" Then\r\nflag = True\r\nEnd If\r\nNext Sheet\r\nIf flag = False Then\r\nWorksheets.Add().Name = \"config\"\r\nEnd If\r\n\r\n'--- Select that config Sheet\r\n\r\nWorksheets(\"config\").Activate\r\n\r\nFor i = 1 To l\r\nsrange = \"A\" & i\r\nRange(srange).Value = str_array(i)\r\nNext\r\n\r\n'--- Now Sort that Column with all values\r\n\r\nsrange = \"A1:\" & srange\r\nRange(srange).Select\r\nActiveWorkbook.Worksheets(\"config\").sort.SortFields.Clear\r\nActiveWorkbook.Worksheets(\"config\").sort.SortFields.Add Key:=Range(\"A1\"), SortOn:=xlSortOnValues, _\r\nOrder:=xlAscending, DataOption:=xlSortNormal\r\nWith ActiveWorkbook.Worksheets(\"config\").sort\r\n.SetRange Range(srange)\r\n.Header = xlNo\r\n.MatchCase = False\r\n.Orientation = xlTopToBottom\r\n.SortMethod = xlPinYin\r\n.Apply\r\nEnd With\r\n\r\n'--- After sorting now Store back all the letters in the array variable\r\n\r\nFor i = 1 To l\r\nsrange = \"A\" & i\r\nstr_array(i) = Range(srange).Value\r\nNext\r\nWorksheets(\"Sheet2\").Activate\r\ninput_str = \"\"\r\nFor i = 1 To l\r\ninput_str = input_str & str_array(i)\r\nNext\r\n\r\n'--- Put the sorted String in your cell where you want\r\n\r\nRange(\"A2\").Value = input_str\r\n\r\n'--- Delete that config file now\r\n\r\nApplication.DisplayAlerts = False\r\nWorksheets(\"config\").Delete\r\nApplication.DisplayAlerts = True\r\n\r\nEnd Sub\r\n\r\n\r\n<\/code><\/pre>\n<\/span>","protected":false},"excerpt":{"rendered":"

Dear Readers, By using Excel Macro, we can sort a String which is stored in a Cell in an Excel Sheet. Sorting will be done as a String. For example (Ascending): First all Numeric Values in ascending Order then Special Chars then Alphabets in ascending etc. For this we need to copy and Paste below […]<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1246,1682],"tags":[],"yoast_head":"\nSorting a String stored in a cell of Excel - Let's excel in Excel<\/title>\n<meta name=\"description\" content=\"By using Excel Macro, we can sort a String which is stored in a Cell in an Excel Sheet. Sorting will be done as a String. For example (Ascending): First all Numeric Values in ascending Order then Special Chars then Alphabets in ascending etc.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sorting a String stored in a cell of Excel\" \/>\n<meta property=\"og:description\" content=\"By using Excel Macro, we can sort a String which is stored in a Cell in an Excel Sheet. Sorting will be done as a String. For example (Ascending): First all Numeric Values in ascending Order then Special Chars then Alphabets in ascending etc.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/\" \/>\n<meta property=\"og:site_name\" content=\"Let's excel in Excel\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:author\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:published_time\" content=\"2011-11-14T18:39:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-04T22:54:30+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/learnexcelmacro.com\/wp\/wp-content\/uploads\/sites\/11\/2016\/12\/5614-Sort-N-Count-RGB-400x400.jpg\" \/>\n<meta name=\"author\" content=\"Vishwamitra Mishra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/www.twitter.com\/learnexcelmacro\" \/>\n<meta name=\"twitter:site\" content=\"@learnexcelmacro\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishwamitra Mishra\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/\"},\"author\":{\"name\":\"Vishwamitra Mishra\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"headline\":\"Sorting a String stored in a cell of Excel\",\"datePublished\":\"2011-11-14T18:39:02+00:00\",\"dateModified\":\"2018-01-04T22:54:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/\"},\"wordCount\":91,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"articleSection\":[\"Excel Macro\",\"Popular Articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/\",\"name\":\"Sorting a String stored in a cell of Excel - Let's excel in Excel\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\"},\"datePublished\":\"2011-11-14T18:39:02+00:00\",\"dateModified\":\"2018-01-04T22:54:30+00:00\",\"description\":\"By using Excel Macro, we can sort a String which is stored in a Cell in an Excel Sheet. Sorting will be done as a String. For example (Ascending): First all Numeric Values in ascending Order then Special Chars then Alphabets in ascending etc.\",\"breadcrumb\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/vmlogger.com\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel Macro\",\"item\":\"https:\/\/vmlogger.com\/excel\/macro\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Sorting a String stored in a cell of Excel\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\",\"url\":\"https:\/\/vmlogger.com\/excel\/\",\"name\":\"Let's excel in Excel\",\"description\":\"Let's share knowledge\",\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/vmlogger.com\/excel\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\",\"name\":\"Vishwamitra Mishra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png\",\"contentUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png\",\"width\":528,\"height\":560,\"caption\":\"Vishwamitra Mishra\"},\"logo\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/\"},\"description\":\"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.\",\"sameAs\":[\"http:\/\/www.learnexcelmacro.com\",\"http:\/\/www.facebook.com\/vmlogger\",\"https:\/\/twitter.com\/https:\/\/www.twitter.com\/learnexcelmacro\",\"https:\/\/www.youtube.com\/c\/VMLogger\"],\"url\":\"https:\/\/vmlogger.com\/excel\/author\/vishwamitra\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Sorting a String stored in a cell of Excel - Let's excel in Excel","description":"By using Excel Macro, we can sort a String which is stored in a Cell in an Excel Sheet. Sorting will be done as a String. For example (Ascending): First all Numeric Values in ascending Order then Special Chars then Alphabets in ascending etc.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/","og_locale":"en_US","og_type":"article","og_title":"Sorting a String stored in a cell of Excel","og_description":"By using Excel Macro, we can sort a String which is stored in a Cell in an Excel Sheet. Sorting will be done as a String. For example (Ascending): First all Numeric Values in ascending Order then Special Chars then Alphabets in ascending etc.","og_url":"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/","og_site_name":"Let's excel in Excel","article_publisher":"http:\/\/www.facebook.com\/vmlogger","article_author":"http:\/\/www.facebook.com\/vmlogger","article_published_time":"2011-11-14T18:39:02+00:00","article_modified_time":"2018-01-04T22:54:30+00:00","og_image":[{"url":"http:\/\/learnexcelmacro.com\/wp\/wp-content\/uploads\/sites\/11\/2016\/12\/5614-Sort-N-Count-RGB-400x400.jpg"}],"author":"Vishwamitra Mishra","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/www.twitter.com\/learnexcelmacro","twitter_site":"@learnexcelmacro","twitter_misc":{"Written by":"Vishwamitra Mishra","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/#article","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/"},"author":{"name":"Vishwamitra Mishra","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"headline":"Sorting a String stored in a cell of Excel","datePublished":"2011-11-14T18:39:02+00:00","dateModified":"2018-01-04T22:54:30+00:00","mainEntityOfPage":{"@id":"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/"},"wordCount":91,"commentCount":2,"publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"articleSection":["Excel Macro","Popular Articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/","url":"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/","name":"Sorting a String stored in a cell of Excel - Let's excel in Excel","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/#website"},"datePublished":"2011-11-14T18:39:02+00:00","dateModified":"2018-01-04T22:54:30+00:00","description":"By using Excel Macro, we can sort a String which is stored in a Cell in an Excel Sheet. Sorting will be done as a String. For example (Ascending): First all Numeric Values in ascending Order then Special Chars then Alphabets in ascending etc.","breadcrumb":{"@id":"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/vmlogger.com\/excel\/2011\/11\/sorting-a-string-stored-in-a-cell-of-excel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vmlogger.com\/excel\/"},{"@type":"ListItem","position":2,"name":"Excel Macro","item":"https:\/\/vmlogger.com\/excel\/macro\/"},{"@type":"ListItem","position":3,"name":"Sorting a String stored in a cell of Excel"}]},{"@type":"WebSite","@id":"https:\/\/vmlogger.com\/excel\/#website","url":"https:\/\/vmlogger.com\/excel\/","name":"Let's excel in Excel","description":"Let's share knowledge","publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vmlogger.com\/excel\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5","name":"Vishwamitra Mishra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/","url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png","contentUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png","width":528,"height":560,"caption":"Vishwamitra Mishra"},"logo":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/"},"description":"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.","sameAs":["http:\/\/www.learnexcelmacro.com","http:\/\/www.facebook.com\/vmlogger","https:\/\/twitter.com\/https:\/\/www.twitter.com\/learnexcelmacro","https:\/\/www.youtube.com\/c\/VMLogger"],"url":"https:\/\/vmlogger.com\/excel\/author\/vishwamitra\/"}]}},"_links":{"self":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/12086"}],"collection":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/comments?post=12086"}],"version-history":[{"count":0,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/12086\/revisions"}],"wp:attachment":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/media?parent=12086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/categories?post=12086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/tags?post=12086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}