What should happen if there is no comma?
What should happen if there are multiple commas?
A little validation before hand is probably a good thing:
Option Explicit
Sub flip3()
Dim temp As Variant
Dim rg As Range
For Each rg In Selection
If Len(rg.Text) _
- Len(Application.Substitute(rg.Text, ",", "")) <> 1 Then
'do nothing
Else
temp = Split97(rg.Text, ",")
rg.Value = temp(UBound(temp)) & " " & temp(LBound(temp))
rg.Value = Trim(rg.Text)
End If
Next rg
End Sub
Function Split97(sStr As String, sdelim As String) As Variant
'from Tom Ogilvy
Split97 = Evaluate("{""" & _
Application.Substitute(sStr, sdelim, """,""") & """}")
End Function
(xl2k added replace, but in xl97, application.substitute can be used.)
CLR wrote:
>
> Interesting Dave, thanks..........but in my XL97, if there is no comma in the
> cell, this macro just doubles the text.........if there are two commas, some
> text gets deleted.....
>
> Vaya con Dios,
> Chuck, CABGx3
>
> "Dave Peterson" wrote:
>
> > split was added in xl2k.
> >
> > Option Explicit
> > Sub flip2()
> > Dim temp As Variant
> > Dim rg As Range
> >
> > On Error Resume Next
> > For Each rg In Selection
> > temp = Split97(rg.Text, ",")
> > rg.Value = temp(UBound(temp)) & " " & temp(LBound(temp))
> > rg.Value = Trim(rg.Text)
> > Next rg
> > on Error goto 0
> >
> > End Sub
> >
> >
> > Function Split97(sStr As String, sdelim As String) As Variant
> > 'from Tom Ogilvy
> > Split97 = Evaluate("{""" & _
> > Application.Substitute(sStr, sdelim, """,""") & """}")
> > End Function
> >
> >
> >
> >
> >
> > CLR wrote:
> > >
> > > Hi Ron........
> > > I'm using XL97 and I get a "Compile error...Sub or function not defined"
> > > error message, highlighting the word "Split"
> > > ....need I set some reference or something?
> > >
> > > Vaya con Dios,
> > > Chuck, CABGx3
> > >
> > > "Ron Rosenfeld" wrote:
> > >
> > > > On Tue, 14 Feb 2006 08:56:36 -0800, Andrea <(E-Mail Removed)>
> > > > wrote:
> > > >
> > > > >Is there a way to lip a name around in a single cell? For example Smith,
> > > > >John to John Smith.
> > > > >
> > > > >Thank you
> > > > >
> > > > >Andrea
> > > >
> > > > This macro will look for a comma, and, if present, return the string after the
> > > > comma, a <space>, then the string before the comma.
> > > >
> > > > <alt-F11> opens the VB Editor.
> > > >
> > > > Ensure your project is highlighted in the project explorer window, then
> > > > Insert/Module and paste the code below into the window that opens.
> > > >
> > > > Select a cell, or range of cells. Then <alt-F8> and select the macro from the
> > > > dialog box that opens and <run>.
> > > >
> > > > ===================
> > > > Sub flip()
> > > > Dim temp() As String
> > > > Dim rg As Range
> > > >
> > > > On Error Resume Next
> > > > For Each rg In Selection
> > > > temp = Split(rg.Text, ",")
> > > > rg.Value = temp(1) & " " & temp(0)
> > > > rg.Value = Trim(rg.Text)
> > > > Next rg
> > > >
> > > > End Sub
> > > > =================
> > > > --ron
> > > >
> >
> > --
> >
> > Dave Peterson
> >
--
Dave Peterson
|