How do you get e.g. "John D. Doe" to "Doe, John D." in the AT using VB Scripting without going individually going through each line? I am using ArcGIS 9.3.

I should note that not all the names are the same; they are different. Is it possible to select those in the column with different names and just reverse the "first, last name" to "last name, first"?

3 Answers 3


One option would be using the field calculator. It does go through every line, but I'm not sure I understand how you would do this without iterating through the table in some manner. I am not good at VB Script, but you can use the Split function to separate based on the first space in the string (assuming all names follow that structure), and it should be something like this:

EDIT: Apologies to the opening poster, I actually got it all wrong. Here is my best attempt at a VB script that will put the last word in a space-delimited string at the beginning with a comma and then add the rest, but I am getting a syntax error. If you can find it, please edit this to be correct! I will continue to poke at it until then.

Function switchpos(ByVal theString As String) As String
    Dim theArray() As String
    Dim theFinal as String
    theArray() = Split(theString, " ", -1)
    For i = LBound(theArray) To UBound(theArray)-1
        theFinal = theFinal & theArray(i)
    switchpos = theArray(UBound(theArray)) & ", " & theFinal
    Return switchpos
End Function

I'm still working on a good method to get the rest of the variable strings added up. If you want a quick stopgap in python, plug the following into your Codeblock:

def nameswitch(theString):
names = theString.Split()
b = ""
for name in names:
    if(name == names[-1]):
    b = b+name+" "
return names[-1]+", "+b

And put this in the execution block:


The previous code took the Table value STOCK LN FREEDOM and returned FREEDOM, STOCK LN.

Is that what you're looking for?

  • Two problems with this: first, the string will only be split at the first occurrence of the delimiter (i.e. "John D. Doe" would be split into "John" and "D. Doe"), which is not helpful in Zoran's case; second, you're not changing the order of the string components, but simply inserting a comma after the first component.
    – nmpeterson
    Commented Nov 9, 2011 at 16:59
  • Oh, my mistake! I completely misread that, somehow. Let me fix that.
    – Nathanus
    Commented Nov 9, 2011 at 17:03
  • i got it now, Split( [ADDR_SN]," ",2)(0) & ", " & Split( [ADDR_SN], " ",2)(1) - I changed the (0) to (1) -- (1) to (0)
    – Zoran
    Commented Nov 9, 2011 at 17:41
  • What I gave you before doesn't switch the names around. I'm working on a VB solution, but there's a python one up for now.
    – Nathanus
    Commented Nov 9, 2011 at 17:42
  • 1
    This is basically the right idea. However, breaking the name into lots of little pieces is inefficient and can destroy information. The core loop can be implemented by (a) extracting the final token as in lastName = theArray(UBound(theArray)) and (b) deleting that token from the end of the original string and concatenating it with what remains, separated by a comma, as in switchpos = lastName & ", " & Replace(theString & "!", lastName & "!", "") The exclamation point is a sentinel; the procedure works best when the sentinel is known not to appear in the string at all.
    – whuber
    Commented Nov 9, 2011 at 19:10

If you're fortnuate enough that every single name is in the format "First M. Last", you'll be able to use ". " as the delimeter, and use the following expression in the Field Calculator:

Split( [NAME],". ")(1) & ", " & Split( [NAME], ". ")(0) & "."

However, if the names are not all in that same format, you will need a more complex method like Nathanus's revised one.


Try this. I might have missed something but I tried to account for the "Middle" section.

Codeblock pre logic:

def swapNames(name):
    nameList = name.split()
    first = nameList[0]
    middle = nameList[1:-1]
    last = nameList[-1]
    z = ""
    for n in middle:
        z += n + " "

    return str(last) + ", " + str(first) + " " + str(z)

Bottom section

swapNames( !nameTest!)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.