使用vba代码在('和')之间提取数据

时间:2016-07-23 18:12:25

标签: excel-vba vba excel

我想使用vba代码在('和')之间提取数据。 例如来自string =" ('128003848885492'), ('128003848885502')"。我需要128003848885492128003848885502并需要将这些值分别与其他文件进行比较。

请建议。

提前感谢。

2 个答案:

答案 0 :(得分:1)

删除不必要的字符,留下逗号分割字符串。这将为您留下一串数字。

Sub Example()

    Dim StringValues  As String
    Dim ArrayValues() As String
    Dim Value As Variant

    StringValues = "('128003848885492'),('128003848885502')"

    'Remove unnecessary characters leaving the commas to split the string
    StringValues = Replace(StringValues, ")", "")
    StringValues = Replace(StringValues, "(", "")
    StringValues = Replace(StringValues, "'", "")

    ArrayValues = Split(StringValues, ",")

    For Each Value In ArrayValues

         Debug.Print CDbl(Value)

    Next

End Sub

enter image description here

答案 1 :(得分:1)

另一种方式,更通用,只分离数字:

Dim str, newstr As String
Dim arr() As String
Dim i As Integer
str = "('128003848885492'), ('128003848885502')"
For i = 0 To Len(str) - 1
   If IsNumeric(Mid(str, i + 1, 1)) Or Mid(str, i + 1, 1) = "," Then
      newstr = newstr & Mid(str, i + 1, 1) 'We extract only the numbers and the comma.
   End If
Next i
arr = Split(newstr, ",")
MsgBox (arr(0)) 'first number
MsgBox (arr(1)) 'second number