Reverse words

Catalina

Registered User.
Local time
Today, 11:12
Joined
Feb 9, 2005
Messages
471
In a single text field I have this:

Phoenix, Tucson, El Paso, Dallas

Is it possible to reverse this with a command so it will be:

Dallas, El Paso, Tucson, Phoenix

The number of words may vary

All help will be greatly appreciated.
Catalina
 
The following function should do the trick:
Code:
[COLOR="Navy"]Public Function[/COLOR] ReverseWords( _
    [COLOR="navy"]ByVal[/COLOR] sInput [COLOR="navy"]As String[/COLOR], _
    [COLOR="navy"]Optional ByVal[/COLOR] sDelimiter [COLOR="navy"]As String[/COLOR] = ", ") _
    [COLOR="navy"]As String[/COLOR]

    [COLOR="navy"]Dim[/COLOR] vInput [COLOR="navy"]As Variant[/COLOR]
    [COLOR="navy"]Dim[/COLOR] vOutput [COLOR="navy"]As Variant[/COLOR]
    [COLOR="navy"]Dim[/COLOR] Y [COLOR="navy"]As Long[/COLOR]

    vInput = Split(sInput, sDelimiter)
    [COLOR="navy"]ReDim[/COLOR] vOutput([COLOR="navy"]UBound[/COLOR](vInput))
    [COLOR="navy"]For[/COLOR] Y = 0 To [COLOR="navy"]UBound[/COLOR](vInput)
        vOutput([COLOR="navy"]UBound[/COLOR](vInput) - Y) = vInput(Y)
    [COLOR="navy"]Next[/COLOR] Y

    ReverseWords = Join(vOutput, sDelimiter)

[COLOR="navy"]End Function[/COLOR]

Test the function with:
Code:
? ReverseWords("Phoenix, Tucson, El Paso, Dallas")
 
Thanks ByteMyzer,

I'll give it a try and let you know how it turns out.

Catalina
 
great stuff ByteMyzer

With a small adjustment:D

dog lazy the over jumped fox brown quick The
 
That should be

dog lazy the over jumps fox brown quick The

...so that you also include the letter S. (It's all right; most people make that mistake) Nice one, Mike ;)
 
That should be

dog lazy the over jumps fox brown quick The

...so that you also include the letter S. (It's all right; most people make that mistake) Nice one, Mike ;)

My altering of the code must have been wrong:D
 
bytemyzer

join?

is that another standard function i wasnt aware of?
 
It is a standard VB/VBA function as of Version 6.0, and is available in A2000+:
Code:
Join([i]SourceArray[/i], [i][Delimiter][/i]) As String
 
It works like a charm.

Thanks again ByteMyzer.

Catalina
 

Users who are viewing this thread

Back
Top Bottom