DAER ALL
I have 3 thou odd names in excel97. The names are entered like
A.S.R.CHARLIE;
P.K.WHITE; M.PETER and so on.
Can u pls sugest VB code so that I can run through each cell and take
these initials and attach it at the end of the name after giving a
space or two like CHARLIE A.S.R; PETER M. and so on. Pls
help.
================================
Option Explicit
Sub MoveInit()
Dim c As Range
Dim oRegex As Object
Dim oMatchCollection As Object
Dim i As Long
Const sPattern As String = "(([A-Z]\.\s?)*)(\w+)"
Set oRegex = CreateObject("VBScript.Regexp")
oRegex.Global = True
oRegex.ignorecase = False
oRegex.Pattern = sPattern
For Each c In Selection
Debug.Print oRegex.Replace(c.Text, "$3 $1")
Next c
End Sub
====================================
However, the above assumes all of your entries consist of initials (capital
letter followed by a dot) followed by a single name.
Do you have other variations?
Such as M.Peter James?
If so, what do you want as a result.
================================
Obviously, instead of printing the results in the immediate window, you might
want to either change the data in place, or print the corrected data in some
other column.
The following will print the data in the adjacent column:
=====================================
Option Explicit
Sub MoveInit()
Dim c As Range
Dim oRegex As Object
Dim oMatchCollection As Object
Dim i As Long
Const sPattern As String = "(([A-Z]\.\s?)*)(\w+)"
Set oRegex = CreateObject("VBScript.Regexp")
oRegex.Global = True
oRegex.ignorecase = False
oRegex.Pattern = sPattern
For Each c In Selection
c.Offset(0, 1).ClearContents
c.Offset(0, 1).Value = oRegex.Replace(c.Text, "$3 $1")
Next c
End Sub
====================================
--ron