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 said:
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 said:
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