Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 115
Default NEED VB TO REMOVE INITIALS FROM A NAME

DEAR ALL
I have in excel-97, some 3 thou odd names. But the names have
initials like A.R.S.CHARLIE; K.T.WHITE; B.PHILIPS and so on. Can you
pls sugest a VB code so that it goes through each cell and removes the
initials and adds it to the end of the name by giving a space -like
CHARLIE A.R.S; WHITE K.T; PHILIPS B. Pls guide
me.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default NEED VB TO REMOVE INITIALS FROM A NAME

Add this macro to any general code module, select the cells to convert, and
run the macro.

Public Sub ConvertNames()

Dim RegEx As Object
Dim Cell As Range
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Global = True
RegEx.MultiLine = True
RegEx.IgnoreCase = True
RegEx.Pattern = "^([a-z..]*\.)([a-z]+)$"

For Each Cell In Selection
Cell = RegEx.Replace(Cell, "$2 $1")
Next Cell

End Sub

Kevin
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default NEED VB TO REMOVE INITIALS FROM A NAME

Kevin - Very interesting solution. Does a RegExp have any kind of a
performance gain (or loss) over using regular VBA functions?

Just a note to CAPTNGNVR this solution does not differentiate between
formulas and values and will overwrite any formulas with values. My solution
assumed you did not want to overwrite formulas.
--
HTH...

Jim Thomlinson


"Kevin Jones" wrote:

Add this macro to any general code module, select the cells to convert, and
run the macro.

Public Sub ConvertNames()

Dim RegEx As Object
Dim Cell As Range
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Global = True
RegEx.MultiLine = True
RegEx.IgnoreCase = True
RegEx.Pattern = "^([a-z..]*\.)([a-z]+)$"

For Each Cell In Selection
Cell = RegEx.Replace(Cell, "$2 $1")
Next Cell

End Sub

Kevin

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default NEED VB TO REMOVE INITIALS FROM A NAME

If used smartly, RegExp does perform quite well. The bulk of the time
consumed is in the instantiating and initializing of the scripting
environment. These two routines do the same function. I ran each on Excel
2003 on 65,000 names. The RegExp version took 3.75 seconds and the InStrRev
version took 5.92 seconds.

Public Sub ConvertNames1()

Dim RegEx As Object
Dim Cell As Range
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Global = True
RegEx.MultiLine = True
RegEx.IgnoreCase = True
RegEx.Pattern = "^([a-z..]*\.)([a-z]+)$"

Dim TimeMark As Double

TimeMark = Timer

For Each Cell In Selection
Cell = RegEx.Replace(Cell, "$2 $1")
Next Cell

MsgBox Timer - TimeMark

End Sub

Public Sub ConvertNames2()

Dim Cell As Range
Dim Pos As Long

Dim TimeMark As Double

TimeMark = Timer

For Each Cell In Selection
Pos = InStrRev(Cell, ".")
If Pos 0 Then
Cell = Mid(Cell, Pos + 1) & " " & Left(Cell, Pos - 1)
End If
Next Cell

MsgBox Timer - TimeMark

End Sub

Kevin

"Jim Thomlinson" wrote:

Kevin - Very interesting solution. Does a RegExp have any kind of a
performance gain (or loss) over using regular VBA functions?

Just a note to CAPTNGNVR this solution does not differentiate between
formulas and values and will overwrite any formulas with values. My solution
assumed you did not want to overwrite formulas.
--
HTH...

Jim Thomlinson


"Kevin Jones" wrote:

Add this macro to any general code module, select the cells to convert, and
run the macro.

Public Sub ConvertNames()

Dim RegEx As Object
Dim Cell As Range
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Global = True
RegEx.MultiLine = True
RegEx.IgnoreCase = True
RegEx.Pattern = "^([a-z..]*\.)([a-z]+)$"

For Each Cell In Selection
Cell = RegEx.Replace(Cell, "$2 $1")
Next Cell

End Sub

Kevin

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default NEED VB TO REMOVE INITIALS FROM A NAME

Very interesting. I used Mid$ and Left$ to tweak up the performance on my
code but you still beat me by a second. I have to use 2 text functions where
as you get away with just one replace function. That is probalby the
difference. I will probably stick with my code as my regexp is distinctly
poor but it is good to know.

Note to CAPTGNVR - Go with Kevin's code as it will work in 97 and mine will
not. If you need to worry about overwriting formulas (probably not an issue
but who knows) then you can grab the first bit of my code to exclude cells
with formulas.
--
HTH...

Jim Thomlinson


"Kevin Jones" wrote:

If used smartly, RegExp does perform quite well. The bulk of the time
consumed is in the instantiating and initializing of the scripting
environment. These two routines do the same function. I ran each on Excel
2003 on 65,000 names. The RegExp version took 3.75 seconds and the InStrRev
version took 5.92 seconds.

Public Sub ConvertNames1()

Dim RegEx As Object
Dim Cell As Range
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Global = True
RegEx.MultiLine = True
RegEx.IgnoreCase = True
RegEx.Pattern = "^([a-z..]*\.)([a-z]+)$"

Dim TimeMark As Double

TimeMark = Timer

For Each Cell In Selection
Cell = RegEx.Replace(Cell, "$2 $1")
Next Cell

MsgBox Timer - TimeMark

End Sub

Public Sub ConvertNames2()

Dim Cell As Range
Dim Pos As Long

Dim TimeMark As Double

TimeMark = Timer

For Each Cell In Selection
Pos = InStrRev(Cell, ".")
If Pos 0 Then
Cell = Mid(Cell, Pos + 1) & " " & Left(Cell, Pos - 1)
End If
Next Cell

MsgBox Timer - TimeMark

End Sub

Kevin

"Jim Thomlinson" wrote:

Kevin - Very interesting solution. Does a RegExp have any kind of a
performance gain (or loss) over using regular VBA functions?

Just a note to CAPTNGNVR this solution does not differentiate between
formulas and values and will overwrite any formulas with values. My solution
assumed you did not want to overwrite formulas.
--
HTH...

Jim Thomlinson


"Kevin Jones" wrote:

Add this macro to any general code module, select the cells to convert, and
run the macro.

Public Sub ConvertNames()

Dim RegEx As Object
Dim Cell As Range
Set RegEx = CreateObject("vbscript.regexp")
RegEx.Global = True
RegEx.MultiLine = True
RegEx.IgnoreCase = True
RegEx.Pattern = "^([a-z..]*\.)([a-z]+)$"

For Each Cell In Selection
Cell = RegEx.Replace(Cell, "$2 $1")
Next Cell

End Sub

Kevin



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
NEED VB TO REMOVE INITIALS FROM A NAME CAPTGNVR Excel Programming 3 July 17th 07 11:48 PM
Need Period After Initials Susan Excel Worksheet Functions 21 May 15th 07 09:45 PM
initials + increment [email protected] Excel Programming 6 July 6th 06 10:44 AM
How to pluck out initials of a 2 or 3 word name Craig Brody Excel Worksheet Functions 1 September 9th 05 03:59 PM
Returning User Initials Kelly[_3_] Excel Programming 10 August 11th 03 07:07 PM


All times are GMT +1. The time now is 05:39 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"