在VBA中将字母数字字符串转换为数字数组

oct*_*ano 1 excel vba

我一直在尝试转换VBA中的单元格,如下所示:

我今年99岁,1918年出生.

进入一个数组,该数组将包含并显示在单元格上:

[99,1918]

这是我的代码:

Sub ConvertColumnToArray(Length As Integer)
    Dim i As Integer, infoArray As Variant
    Set Column = Sheets("Short Desc")
    For i = 2 To Length
        infoArray = StringToIntArray(Column.Range("A" & i))
        Column.Range("A" & i) = "[" & infoArray(0) & "," & infoArray(1) & "]"
    End For
End Sub

Function StringToIntArray(str As String) As Variant
    'the code to add
End Function
Run Code Online (Sandbox Code Playgroud)

GSe*_*erg 5

Function StringToIntArray(str As String) As Variant
  Static regex As Object 'VBScript_RegExp_55.RegExp

  If regex Is Nothing Then
    Set regex = CreateObject("VBScript.RegExp")  ' New VBScript_RegExp_55.RegExp
    regex.Global = True
    regex.Pattern = "\d+"
  End If

  Dim matches As Object ' MatchCollection
  Set matches = regex.Execute(str)

  If matches.Count > 0 Then
    Dim result() As Long
    ReDim result(0 To matches.Count - 1)

    Dim i As Long
    For i = 0 To matches.Count - 1
      result(i) = CLng(matches(i).Value)
    Next

    StringToIntArray = result
  End If

End Function
Run Code Online (Sandbox Code Playgroud)