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

时间:2018-11-06 11:03:05

标签: 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

4 个答案:

答案 0 :(得分: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

答案 1 :(得分:4)

如果您决定避免使用RegEx,那么这是一种更快,更有效的解决方案:

Option Explicit

Sub TestMe()

    Dim stringInput As String
    stringInput = "I am 99 years old and I was born in 1918"
    Debug.Print Join(StringToIntArray(stringInput), " ")

End Sub

Function StringToIntArray(myInput As String) As Variant

    Dim i As Long
    Dim infoArray As Variant
    Dim unitArray As Variant
    Dim result As Variant

    infoArray = Split(myInput)

    For Each unitArray In infoArray
        If IsNumeric(unitArray) Then
            result = result & " " & unitArray
        End If
    Next
    StringToIntArray = Split(Trim(result))

End Function
  • 它将字符串按空格分隔为一个数组-infoArray
  • 对于每个单位,如果单位为数字,则将其添加到字符串result中。
  • 通过result操作将字符串StringToIntArray解析为Split()

如前所述,如果.末尾有一个stringInput,例如“ ... in1918。”,则不会考虑年份“ 1918”,因为它是“ 1918”。它不是数字。因此,必须执行附加的“剥离”,剥离点。

答案 2 :(得分:4)

只是一脚踢,如果您具有带有TEXTJOIN函数的Excel 2016,则可以使用以下 array 公式:

=TEXTJOIN(",",TRUE,IFERROR(--(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),""))

其中

seq_99 refers to:  =IF(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))=1,1,(ROW(INDEX($1:$65535,1,1):INDEX($1:$65535,255,1))-1)*99)

要输入/确认数组公式,请在按下 enter 的同时按住 ctrl + shift 。如果正确执行此操作,Excel会在公式栏中显示的公式周围放置括号{...}

enter image description here

或者,就像我在代码中看到的那样显示方括号:

=CONCAT("[",TEXTJOIN(",",TRUE,IFERROR(--(MID(SUBSTITUTE(TRIM(A1)," ",REPT(" ",99)),seq_99,99)),"")),"]")

如果我要使用VBA,我会使用类似的东西:

Function StringToIntArray(str As String) As String
    Dim v, w, s As String

v = Split(str)
For Each w In v
    If IsNumeric(w) Then
        s = s & "," & w
    End If
Next w

StringToIntArray = "[" & Mid(s, 2) & "]"

End Function

答案 3 :(得分:3)

考虑:

Public Function StringToIntArray(str As String) As Variant
    Dim temp As String, i As Long, L As Long
    Dim CH As String
    Dim wf As WorksheetFunction
    Set wf = Application.WorksheetFunction

    temp = ""
    L = Len(str)
    For i = 1 To L
        CH = Mid(str, i, 1)
        If CH Like "[0-9]" Then
            temp = temp & CH
        Else
            temp = temp & " "
        End If
    Next i

    StringToIntArray = Split(wf.Trim(temp), " ")

End Function

例如:

Sub MAIN()
    Dim s As String, arr, a, output As String
    s = "qwerty45bgt567cdrew098"

    arr = StringToIntArray(s)
    output = ""

    For Each a In arr
        output = output & a & vbCrLf
    Next a
    MsgBox output

End Sub

enter image description here

该代码不依赖于[ space ]字符。它将返回输入字符串的所有数字子字符串的数组。