Re: Use of Like to extract data



On 11 Mar 2007 05:51:40 -0700, rve_52@xxxxxxxxxxx wrote:

I have to process a large number of cells looking for various strings
consisting of numbers followed by a decimal. The strings can consist
of 1, 2 or 3 digits then the decimal. I need to extract the string
if

I find it. I have tried this approach looking for a single digit and
a decimal.

strCall = ActiveCell.Value2
For iCnt = 1 To Len(strCall)
If (Mid(strCall, iCnt, 1) Like "*[0-9,.]*") Then
MsgBox iCnt
MsgBox Mid(strCall, iCnt, 2)
Exit For
End If
Next iCnt

Is there a better/faster way that I can do it without looping through
the string containing the data character by character?

Thanks for your help.

Here's one way.

You can use late binding but for maximum speed I would set a reference
(Tools/References) to Microsoft VBScript Regular Expressions 5.5.

================================
Sub test()
Dim s(3) As String
Dim i As Long

s(0) = "abc12.xy"
s(1) = "123.abc"
s(2) = "12ab456.xyz"
s(3) = "12345." 'not sure what you want here

Dim oRegex As RegExp
Dim oMatch As Match
Dim colmatches As MatchCollection
Const sPattern As String = "\d{1,3}(?=\.)"

Set oRegex = New RegExp
oRegex.Pattern = sPattern
oRegex.Global = True

For i = 0 To UBound(s)
If oRegex.test(s(i)) = True Then
Set colmatches = oRegex.Execute(s(i))
Debug.Print colmatches(0)
End If
Next i

End Sub
==============================
--ron
.



Relevant Pages

  • Re: Defining a numeric system
    ... The only limit to the number of "digits" in the number is Excel's restriction on the number of characters that can be placed in a cell. ... Function AddOne(Number As String, ... Dim Char() As Byte ... ToBase As Integer, ...
    (microsoft.public.excel)
  • Re: Hexadecimal to Binary Conversion
    ... Optional NumberOfDigits As Integer) As String ... 'If NumberOfDigits is set to 0, left out or set to fewer digits ... Dim ToBaseTen As Long ... If ToBase> LenThen Exit Function ...
    (microsoft.public.excel.programming)
  • Re: Custom cell formats
    ... In addition, since your account numbers are 19 digits long, the entry ... Const sZEROS As String = "0000000000000000000" ... Dim vDigits As Variant ... Dim nStart As Long ...
    (microsoft.public.excel.misc)
  • Re: How do I convert a number into words in MS Word 2003?
    ... Function ConvertCurrencyToEnglish(ByVal MyNumber) ... Dim DecimalPlace, Count ... ReDim PlaceAs String ... 'convert last 3 digits to English Dollars ...
    (microsoft.public.word.vba.general)
  • Re: Dynamic IF or Select Case
    ... > Dim X As Long ... > Dim AnswersAs String ... For Each vFile In vFiles ... Dim sTmp As String, iCnt As Integer ...
    (microsoft.public.vb.general.discussion)