如何从vba中的string中提取数字组

我有一个如下形状的string:

RRP 90 AVE DE GAULLE 92800 PUTEAUX 0109781431-0149012126 

数字可能由连字符(例如空格)分隔。 我知道如何用len()来区分它们。

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

我发现如何从string中去除字符: 如何从string中find数字?

但是这不适合我的问题…

你能指导我一个函数或一点代码,可以帮助我吗?

这将比循环运行快得多

 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 

尝试下面的代码:

 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 

所以我意识到这是很久以前…但我正在寻找一个类似的解决scheme在线。

我的编程技巧(原文如此)的一些以前的历史:我开始与Python和Python我有一个方便的工具称为List 。 VBA没有这个,所以我留下来的东西,我可以input一个variables,我称为下面的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

在我运行代码之后。

如果在使用@Scotts时遇到错误“编译时出现问题,用户定义types未定义”答案启用正则expression式选项,如步骤1和2中所示: 如何使用/启用(RegExp对象)使用VBA的正则expression式MACRO)在Word中 (在Excel中也可以)

Ps Scotts Solution为我工作得很好。