Hello,
This is bit tricky, I have to reverse what's on either side of a " ", but leave anything to the right of a comma alone. And the comma always appears after the words separated by space.
THis works, but seems unnecesarily involved...
Sub Reverse_Spc()
'Each cell begins with two surnames, separated by a space. These names have to be reversed without
'affecting the what's to the right of the comma i.e. first name
Dim i As Long, j As Long, k As Long, varVal As Variant, Sur1 As Variant, Sur2 As Variant, strRemain
For j = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
varVal = Cells(j, 1).Value
For i = 1 To Len(varVal)
If Mid(varVal, i, 1) = "," Then
Exit For
ElseIf Mid(varVal, i, 1) = " " Then
Sur1 = Mid(varVal, 1, i - 1)
For k = i + 1 To Len(varVal)
If Mid(varVal, k, 1) = "," Then
Sur2 = Mid(varVal, i + 1, k - (i + 1))
strRemain = Mid(varVal, k, Len(varVal))
Exit For
End If
Next k
Cells(j, 1).Value = Sur2 & " " & Sur1 & strRemain
End If
Next i
Next j
End Sub
This is bit tricky, I have to reverse what's on either side of a " ", but leave anything to the right of a comma alone. And the comma always appears after the words separated by space.
THis works, but seems unnecesarily involved...
Sub Reverse_Spc()
'Each cell begins with two surnames, separated by a space. These names have to be reversed without
'affecting the what's to the right of the comma i.e. first name
Dim i As Long, j As Long, k As Long, varVal As Variant, Sur1 As Variant, Sur2 As Variant, strRemain
For j = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
varVal = Cells(j, 1).Value
For i = 1 To Len(varVal)
If Mid(varVal, i, 1) = "," Then
Exit For
ElseIf Mid(varVal, i, 1) = " " Then
Sur1 = Mid(varVal, 1, i - 1)
For k = i + 1 To Len(varVal)
If Mid(varVal, k, 1) = "," Then
Sur2 = Mid(varVal, i + 1, k - (i + 1))
strRemain = Mid(varVal, k, Len(varVal))
Exit For
End If
Next k
Cells(j, 1).Value = Sur2 & " " & Sur1 & strRemain
End If
Next i
Next j
End Sub