Link to home
Create AccountLog in
Avatar of gisvpn
gisvpnFlag for United States of America

asked on

Switch around characters within a string (VBA)

Hello  I have a string which always outputs

Surname, firstname


I would like to change the string to Firstname Surname using VBA. Changing around the firstname and surname and removing the ( , ) as well.

Can someone help with this ?

Thanks,

GISVPN
Avatar of sam_norian
sam_norian

Try This:

        Dim OLDString As String
        Dim NewString As String
        Dim commaPos As String
        Dim SirName As String
        Dim FirstName As String

        OLDString = "Criddle, Natalie" 'Existing String

        commaPos = InStr(4, OLDString, ",", 1) 'Get Comma Position

        SirName = Left(OLDString, Val(commaPos - 1)) 'Get Sirname

        FirstName = Right(OLDString, Val(OLDString.Length - commaPos)) ' Get First Name

        NewString = FirstName & " " & SirName

        MsgBox(NewString)
Avatar of gisvpn

ASKER

Hi thanks for the help - I am getting an erorr on this line - im not sure why

  FirstName = Right(OLDString, Val(OLDString.Length - commaPos)) ' Get First Name

Object required ?
what version of vb are you using?
Avatar of gisvpn

ASKER

this is VBA in Word
ASKER CERTIFIED SOLUTION
Avatar of sam_norian
sam_norian

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of gisvpn

ASKER

perfect thanks :)
No probs, thanks for the grade.