View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Format a sorted character string

Maybe something like this as a formula in a worksheet:

=SUBSTITUTE(TRIM(SUBSTITUTE(A1,","," "))," ",",")

In code:
Option Explicit
Sub testme()
Dim myStr As String
myStr = ",,,V01234, V02345, 12346, ,12347,"

myStr = Replace(myStr, ",", " ")
myStr = Application.Trim(myStr)
myStr = Replace(myStr, " ", ",")

MsgBox myStr

End Sub

VBA's Trim() doesn't do anything with those multiple internal spaces. But
application.trim() (the worksheet function), does.



owlnevada wrote:

I have a subroutine that results in different string lengths sorted in
ascending order that come from the tabnames in a workbook like the following:

12345,
,12345, 12346, ,
12345,,12346, 12347,
V01234, V02345, 12346, 12347,

When a leading comma appears, it's from a tabname that exists but doesn't
meet the criteria for the string and the string ends with a comma. How do I
ReDim or reformat the string to eliminate the leading, trailing and
intermediate double commas? Also, if only one tabname appears in the string,
I want to delete that one entirely. The items with V can sort to the end of
the string or if with a leading zero and the V is absent, then they sort to
the beginning. . . The list should look like a standard CSV list in a string
like line 3 above but no ending or intermediate comma.


--

Dave Peterson