Forum Discussion

kconnors100's avatar
kconnors100
Copper Contributor
May 24, 2025

New VBA person - Code not working

Hi,

Using contributions from various snippets of code, I put togerther the following to swap first name last name to last name, first name.

Problem: Regardless of the Column that I identify, it only works for data in A1 (and even then, at times it fails). If anyone can help, please let me know. thanks, Karen. PS I may have attempted something too ambitious for my novice level but I wanted to give it a try (and it would be great for what I need to do at work).

Strangley enough, at least for me, even when I identify a column other than Column A, it will run using data from Column A instead of the identified Column

  1. I use a local version of Excel 365
  2. Excel spreadsheets are also held on local drive
  3. Windows - updated
  4. I need a macro that:
    1. swaps first name last name to last name, first name
    2. ignores Row 1 because that has header info
    3. I need it to loop until there is no more data in the identified column

Here's what I stitched together and again, I may have tried something too ambitious for my current skill level so greatly appreciate any insights . . . thanks, Karen

 

Sub Reorder_To_LastName_FirstName()

    Dim rng As range

    Dim i As Long

    Dim strName As String

    Dim strFirstName As String

    Dim strLastName As String

    Dim arrNames() As String

' Replace "A1" with the first cell in the column you want to use your name column

' Replaced "A1" with "C1:"

Set rng = range("C1").CurrentRegion

' Identify the row with the first name entry

' For this purpose, first name is in row 2

    For i = 2 To rng.Rows.count

' Verify that the row has data

      If rng.Cells(i, 1).Value <> "" Then

' Pull full name

        strName = rng.Cells(i, 1).Value

' Split the name based on location of space

        arrNames = Split(strName, " ")

' Allocate first position to be the last name in array and first name to be second element in the array

        strLastName = arrNames(0)

        strFirstName = ""

' Loop

        For X = 1 To UBound(arrNames)

          strFirstName = strFirstName & " " & arrNames(X)

        Next X

' Trim to remove unneeded spaces

        strFirstName = Trim(strFirstName)

        strLastName = Trim(strLastName)

' Set order of Last Name, First Name

        rng.Cells(i, 1).Offset(0, 2).Value = strLastName & ", " & strFirstName

        End If

      Next i

    End Sub

4 Replies

  • kconnors100's avatar
    kconnors100
    Copper Contributor

    Thanks so much for your reply. Unfortunately, the requirement from above was to use VBA (long story). I tried using "Record Macro" and using the formulat to try and get it into VBA but then I went down the "rabbit" hole because, again, from above, the requirement was to add "extras". In any case, again, thank you for the follow up. . .  and just in case the question is: "why don't you explain it to the folks "above", tried that and it was easier for me to start learning VBA (I guess something good always comes from "bad"). Best wishes, KC

    • mathetes's avatar
      mathetes
      Silver Contributor

      Got it. I still might push back a bit, especially if you can make a formula (or set of formulas, perhaps coupled with a Pivot Table) that works.

      Short story to illustrate: back when I was employed (I retired over 20 years ago), I was for a time the Director of the HR and compensation database for a major corporation. One of our responsibilities was to produce a monthly headcount report--by division, how many people in each employment category (Full time, part time, temp, etc)--and how have numbers changed since prior month. The company's IT experts could only conceive of doing this by means of a major VBA routine that took data extracted from the DB2 database and massaged it to produce the results. It took a solid 30 minutes to run. I used built-in Excel (at the time it may have been Lotus 1-2-3) formulas and functions to yield the same result almost instantaneously. 

      Lesson: there's a mis-apprehension that "big jobs" require VBA, an apprehension that may sometimes be accurate, but often fails to appreciate the power that has already been "programmed" into the functions themselves.

  • mathetes's avatar
    mathetes
    Silver Contributor

    Well, let me encourage you to discern when VBA is really necessary; by "really necessary" I mean learn to rely on simple formulas when they'll work, probably more efficiently and reliably, as in the attached,

     

Resources