1

我有以下形状的字符串:

RRP 90 AVE DE GAULLE 92800 PUTEAUX 0109781431-0149012126

这些数字可能由连字符以外的其他字符(例如空格)分隔。我知道之后如何用 len() 来区分它们。

我需要单独存储每个数字字符串(例如在数组中),以便我可以用 len() 区分它们,然后使用它们。

我找到了如何从字符串中去除字符: 如何从字符串中查找数字?

但这不适合我的问题...

你能指导我找到一个可以帮助我的函数或代码吗?

4

5 回答 5

2

这将比循环运行快得多

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
于 2013-03-12T19:48:54.553 回答
1

试试下面的代码:

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
于 2013-03-12T19:11:36.047 回答
0

所以我意识到这是很久以前的事了......但我在网上寻找类似的解决方案。

关于我的编程技能的一些以前的历史(原文如此):我从 Python 开始,使用 Python 我有一个方便的工具,叫做List. VBA 没有这个,所以我剩下的是我可以在sample下面调用的变量中输入的东西,即sample = [1,4,5].

回到小代码。我使它holder只包含数字组,就像您指定它们应该如何分组一样。

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

我得到的输出是

1

33

123

23423

123124

在我运行代码之后。

于 2014-07-24T23:34:57.027 回答
0

上面非常简单的 Python 样式循环。

扩展为 A 列中的字符串列表。

发现的数字将显示在右侧的列中 - B、C 等。

Dim count, count1 As Integer
Dim holder As String
Dim sample, smallSample As String
Dim r As Integer
Dim c As Integer
r = 1
c = 1


Do While Sheet2.Cells(r, c) <> ""
    count = 0
    count1 = 1
    sample = Sheet2.Cells(r, c)
    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
                Sheets(2).Cells(r, c + count1).Value = holder
                count1 = count1 + 1
            End If
            holder = ""
        End If
        sample = Right(sample, Len(sample) - 1)
    
    Loop
    r = r + 1
Loop
于 2021-09-06T06:22:02.983 回答
-1

如果您在使用 @Scotts 时收到错误“编译时出现问题,未定义用户定义的类型”答案启用正则表达式选项,如此处的第 1 步和第 2 步所示:如何使用/启用(RegExp 对象)使用 VBA 的正则表达式( MACRO)在word中(也适用于Excel)

Ps Scotts 解决方案对我来说效果很好。

于 2017-06-14T10:43:33.770 回答