How to extract groups of numbers from a string in vba

后端 未结 4 997
栀梦
栀梦 2020-12-01 23:00

I have a string of the following shape:

RRP 90 AVE DE GAULLE 92800 PUTEAUX 0109781431-0149012126

The numbers might be seperated by other ch

相关标签:
4条回答
  • 2020-12-01 23:29

    This will run much faster than looping

    Public Function NumericOnly(s As String) As String
        Dim s2 As String
        Dim replace_hyphen As String
        replace_hyphen = " "
        Static re As RegExp
        If re Is Nothing Then Set re = New RegExp
        re.IgnoreCase = True
        re.Global = True
        re.Pattern = "[^0-9 -]" 'includes space, if you want to exclude space "[^0-9]"
        s2 = re.Replace(s, vbNullString)
        re.Pattern = "[^0-9 ]"
        NumericOnly = re.Replace(s2, replace_hyphen)
    End Function
    
    0 讨论(0)
  • 2020-12-01 23:50

    Try below code :

    Function parseNum(strSearch As String) As String
    
       ' Dim strSearch As String
        'strSearch = "RRP 90 AVE DE GAULLE 92800 PUTEAUX 0109781431-0149012126"
    
        Dim i As Integer, tempVal As String
        For i = 1 To Len(strSearch)
            If IsNumeric(Mid(strSearch, i, 1)) Then
                tempVal = tempVal + Mid(strSearch, i, 1)
            End If
        Next
    
        parseNum = tempVal
    End Function
    
    0 讨论(0)
  • 2020-12-01 23:51

    So I realize this was a long time ago... but I was looking for a similar solution online.

    Some previous history on my programming skillz (sic): I started with Python and with Python I have a handy tool called List. VBA doesn't have this, so what I'm left with is something that I can input in a variable I called sample below, i.e. sample = [1,4,5].

    Back to the small code. I made it so holder would only contain groups of numbers, as how you specified they should be grouped.

    Dim count, count1 As Integer
    Dim holder As String
    Dim sample, smallSample As String
    
    
    count = 0
    count1 = 1
    sample = "1ab33 efa 123 adfije-23423 123124-23423"
    holder = ""
    Do While count <> Len(sample)
        smallSample = Left(sample, 1)
        If smallSample = "0" Or smallSample = "1" Or smallSample = "2" Or smallSample = "3" Or smallSample = "4" Or smallSample = "5" Or smallSample = "6" Or smallSample = "7" Or smallSample = "8" Or smallSample = "9" Then
            holder = holder & smallSample
        Else
            If holder <> "" Then
                Cells(count1,1) = holder
                count1 = count1 + 1
            End If
            holder = ""
        End If
        sample = Right(sample, Len(sample) - 1)
    
    Loop
    

    The output I got was

    1

    33

    123

    23423

    123124

    after I ran the code.

    0 讨论(0)
  • 2020-12-01 23:54

    If you are getting the Error "Issue while compiling, user defined type not defined" while using @Scotts Answer Enable the Regular Expression option as seen in Step 1 and 2 here: How to Use/Enable (RegExp object) Regular Expression using VBA (MACRO) in word (Works in Excel also)

    P.s. Scotts Solution worked well for me.

    0 讨论(0)
提交回复
热议问题