如何获取excel中两个星号之间的值?

时间:2013-07-11 14:00:37

标签: excel vba excel-vba

如何在excel中获取两个星号之间的值?

例如:

我有这些:TXI*GS*346.32*13*SP*ON*3***103634408RT0001

我只想得到值346.32

我这些数据对于A1:A20如何使用VBA替换它们?

2 个答案:

答案 0 :(得分:2)

Sub useSplit()
    Dim s As String
    s = "TXI*GS*346.32*13*SP*ON*3***103634408RT0001"
    Dim a() As String
    a = Split(s, "*")

    Dim i As Long
    For i = 0 To UBound(a)
        Debug.Print a(i)
    Next
End Sub

答案 1 :(得分:0)

Sub extractFirstNumber()

    Set objRegEx = CreateObject("vbscript.regexp")
    ' Match numbers of the form 123 or 123.45
    objRegEx.Pattern = "\d+(\.\d*)?"

    ' Iterate over the first 20 rows in first column
    For i = 1 To 20
        Set objMatch = objRegEx.Execute(Cells(i, 1).Value)
        If objMatch.Count = 1 Then
            ' If there is a match, replace the cell value
            Cells(i, 1).Value = objMatch(0)
        End If
    Next i

End Sub

修改

Sub extractFirstAndThirdNumber()

    Set objRegEx = CreateObject("vbscript.regexp")
    ' Match numbers of the form 123 or 123.45 between asteriks
    objRegEx.Pattern = "\*(\d+(\.\d*)?)"
    objRegEx.Global = True

    ' Iterate over the first 20 rows in first column
    For i = 1 To 20
        Set objMatch = objRegEx.Execute(Cells(i, 1).Value)
        If objMatch.Count >= 2 Then
            ' If there is a match, replace the cell value
            'Cells(i, 1).Value = objMatch(0)
            Cells(i, 3).Value = objMatch(0).SubMatches(0)
            Cells(i, 4).Value = objMatch(2).SubMatches(0)
        End If
    Next i

End Sub