LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #7   Report Post  
Harlan Grove
 
Posts: n/a
Default

"Gord Dibben" <gorddibbATshawDOTca wrote...
Copy the entire column and run this macro on that copy.

OR run it on the original column if you don't care to preserve the alphas.

Sub RemoveAlphas()
'' Remove alpha characters from a string.
Dim intI As Integer
Dim rngR As Range, rngRR As Range
Dim strNotNum As String, strTemp As String
Set rngRR = Selection.SpecialCells(xlCellTypeConstants, xlTextValues)
For Each rngR In rngRR
strTemp = ""
For intI = 1 To Len(rngR.Value)
If Mid(rngR.Value, intI, 1) Like "[0-9,.]" Then
strNotNum = Mid(rngR.Value, intI, 1)
Else: strNotNum = ""
End If
strTemp = strTemp & strNotNum
Next intI
rngR.Value = strTemp
Next rngR
End Sub

....

Inefficient. Also questionable including commas.

First pass, simplify the If block, access the range's .Value property only
once, and call Mid only once per iteration.


For Each r In rng
v = r.Value
t = ""
For i = 1 To Len(v)
c = Mid(v, i, 1)
If c Like "[0-9.]" Then t = t & c
Next i
r.Value = t
Next r


Second pass, eliminate the 1-char temp variable.


For Each r In rng
v = r.Value
For i = 1 To Len(v)
If Mid(v, i, 1) Like "[!0-9.]" Then Mid(v, i, 1) = " "
Next i
r.Value = Application.WorksheetFunction.Substitute(v, " ", "")
Next r


But for maximum flexibility on systems with Windows Script Host installed
(so 99.44% of PCs running Windows 98SE or 2K or later, and most running
Windows NT4), nothing beats regular expressions.


Sub foo()
Dim rng As Range, c As Range, re As Object

Set rng = Selection.SpecialCells( _
Type:=xlCellTypeConstants, _
Value:=xlTextValues _
)

Set re = CreateObject("vbscript.regexp")
re.Pattern = "[^0-9.]+" 'or use an InputBox to set
re.Global = True

For Each c In rng
c.Formula = re.Replace(c.Formula, "")
Next c
End Sub


 
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
Extract hyperlink string from excel cell Ryan Sapien Links and Linking in Excel 1 January 20th 05 12:24 AM
Paste rows of numbers from Word into single Excel cell BecG Excel Discussion (Misc queries) 1 December 8th 04 04:55 PM
Extract date from cell Eric Excel Worksheet Functions 3 November 4th 04 06:37 PM
how to count the nr of occurrences of a text string in a cell rang eagerbuyer Excel Worksheet Functions 1 November 4th 04 12:27 PM
Add a string to a cell ramsdesk Excel Worksheet Functions 2 October 28th 04 07:20 AM


All times are GMT +1. The time now is 05:31 PM.

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"