Rearrange a cell with multiple values alphabetically [Excel] -
i have excel file have cell multiple values, split comma. instance: new york, chicago, los angeles. want rearrange cell in alphatical order, like: chicago, los angeles, new york. there easy way this?
try this. following code read values in column a
, give desired result in column b
.
sub sortstring() dim myarray variant, varswap variant dim long, min long, max long, lastrow long dim str string dim myrange range dim isswapped boolean lastrow = cells(rows.count, "a").end(xlup).row set myrange = range("a1:a" & lastrow) each cell in myrange myarray = split(cell.value, ",") min = lbound(myarray) max = ubound(myarray) - 1 isswapped = false = min max if myarray(i) > myarray(i + 1) varswap = myarray(i) myarray(i) = myarray(i + 1) myarray(i + 1) = varswap isswapped = true end if next max = max - 1 loop until not isswapped = lbound(myarray) ubound(myarray) debug.print myarray(i) if str = "" str = trim(myarray(i)) else str = str & ", " & trim(myarray(i)) end if next cell.offset(0, 1).value = str str = "" next cell end sub
Comments
Post a Comment