【问题标题】:How to extract groups of numbers from a string in vba如何从vba中的字符串中提取数字组
【发布时间】:2021-09-06 14:22:02
【问题描述】:

我有一个如下形状的字符串:

RRP 90 AVE DE GAULLE 92800 PUTEAUX 0109781431-0149012126

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

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

我找到了如何从字符串中去除字符: How to find numbers from a string?

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

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

【问题讨论】:

  • 正则表达式...
  • 您需要在结果中的数字组之间进行一些分隔吗?结果是“909280001097814310149012126”还是需要“90 92800 0109781431 0149012126”?
  • 我已经更新了我的问题

标签: excel vba


【解决方案1】:

这将比循环运行快得多

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

【讨论】:

  • 这个没有任何回报,我正在阅读正则表达式以了解原因。
  • 使用此代码RRP 90 AVE DE GAULLE 92800 PUTEAUX 0109781431-0149012126 通过键入=numericonly(B1) 返回90 92800 01097814310149012126,其中文本字符串位于B1
  • 谢谢,这样就可以了……但是,最后一个字符串需要在连字符所在的地方分开……
  • 试试我的编辑,这应该为连字符创建一个空格,您需要将任何其他字符变成类似于连字符的空格,然后将其添加到"[^0-9 -]" 列表中
  • 看来这不是我所需要的,我仍然无法自己找出答案。我仍然需要能够将我的字符串分离为一个字符串数组,以便我可以处理它的每个部分。此外,我需要添加除连字符之外的其他字符(例如“//”或“/”)作为分隔符,但我无法弄清楚您的实际操作以及如何替换它。
【解决方案2】:

试试下面的代码:

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

【讨论】:

  • @Chipsgoumerde 您希望数字如何显示?要添加它们吗?
  • nop,它不是用于计算,而是在另一个工作表/列中搜索数字。澄清一下:我会检查每组数字:如果是 9 位数字> 在 A 列中搜索/如果是 10 位数字,在 B 列中搜索它
  • @Chipsgoumerde 你想如何对数字进行分组?
  • RRP 90 AVE DE GAULLE 92800 PUTEAUX 0109781431-0149012126 => 我想要 90 / 92800 / 0109781431 / 0149012126
【解决方案3】:

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

关于我的编程技能的一些历史记录(原文如此):我从 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

在我运行代码之后。

【讨论】:

    【解决方案4】:

    上面很简单的 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
    

    【讨论】:

      【解决方案5】:

      如果您在使用 @Scotts Answer 时收到错误“编译时出现问题,未定义用户定义的类型”,请启用正则表达式选项,如此处的第 1 步和第 2 步所示:How to Use/Enable (RegExp object) Regular Expression using VBA (MACRO) in word(也适用于 Excel)

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-22
        相关资源
        最近更新 更多