Results 1 to 3 of 3

Thread: Extract Numeric Value From Alphanumeric Text Using VBA RegExp

  1. #1
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10

    Lightbulb Extract Numeric Value From Alphanumeric Text Using VBA RegExp

    In continuation to the Excel formula based solution given at http://www.excelfox.com/forum/f13/ex...eric-text-186/, here's one using VBA regular expression

    Code:
    Function ExtractNumFromAlphaNum(strAlNum As String) As Long
    
        Const strPatter As String = "[\d+]"
        Dim var, strOutput As String
        
        With CreateObject("vbscript.regexp")
            .Pattern = strPatter
            .Global = True
            Set var = .Execute(strAlNum)
            For Each var In var
                strOutput = strOutput & var
            Next var
        End With
        
        ExtractNumFromAlphaNum = strOutput
        
    End Function
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #2
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    662
    Rep Power
    13
    Quote Originally Posted by Excel Fox View Post
    In continuation to the Excel formula based solution given at http://www.excelfox.com/forum/f13/ex...eric-text-186/, here's one using VBA regular expression

    Code:
    Function ExtractNumFromAlphaNum(strAlNum As String) As Variant
      ...<<snip>>
    End Function
    You should change your function's declared data type to Variant as I show in red above in order to stop the error that is generated when an argument containing no digits is passed into the function.

    From my standpoint, I do not think using the heavy "machinery" of the Regular Expression engine is necessary for such a simple requested functionality; here is a Function using standard built-in VBA functions that will do the same thing (note I changed the function's name to something easier to type)...
    Code:
    Function GetDigits(strAlNum As String) As Variant
      Dim X As Long
      For X = 1 To Len(strAlNum)
        If Mid(strAlNum, X, 1) Like "#" Then GetDigits = GetDigits & Mid(strAlNum, X, 1)
      Next
    End Function

  3. #3
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    I like the way you term it 'heavy machinery'... I completely agree.... It's just that I wanted to share a simple method to show how this bazooka can be used.
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

Similar Threads

  1. Extract Number From Alphanumeric Text
    By Excel Fox in forum Excel and VBA Tips and Tricks
    Replies: 10
    Last Post: 09-11-2013, 10:14 PM
  2. Extracting Numeric Values From Alphanumeric Text
    By Safal Shrestha in forum Excel Help
    Replies: 3
    Last Post: 03-21-2013, 12:04 PM
  3. Replies: 2
    Last Post: 09-24-2012, 09:20 PM
  4. Extract numbers from alphanumeric values
    By tushar.tarafdar in forum Excel Help
    Replies: 3
    Last Post: 09-20-2012, 10:16 PM
  5. Replies: 2
    Last Post: 02-29-2012, 08:24 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •