2016-11-22 68 views
0

我在Excel中有這個問題,我需要從下面的一個文本字符串中提取數據。以下數據全部在單獨的行上。我試圖只提取斜槓之間的數據,如x/y/z/a/b/c格式,並忽略前面的其他數據。希望這個問題可以使用vba解決。如何提取數據

一個例子:

s1 167.4/164.2/172.0/165.8/162.5/165.6 s2 163.6/164.1/167.2/167.1/165.6/167.6 S#1:0.1682/0.1646/0.1641/0.1653/0.1622/0.1642 S#2:0.1623/0.1621/0.1667/0.1680/0.1667/0.1643 s24=1903/1881/1873/1903/1915/1891 s25=1872/1858/1904/1879/1854/1868 SLOT 24:0.2031/0.1991/0.1959/0.1989/0.1987/0.2051 SLOT 25:0.2014/0.2019/0.2014/0.2033/0.2001/0.2017

+0

預期的結果是什麼? – Andreas

+0

我只想要斜槓之間的數字,所以's1 167.4/164.2/172.0/165.8/162.5/165.6'會變成'167.4/164.2/172.0/165.8/162.5/165.6' – JohnSmith

回答

0

看一看這樣的:

Public Sub RegExpExample() 
    Dim rng As Range 
    Dim c As Variant, arr As Variant 
    Dim k As Long 
    Dim regEx As Object 
    Dim Match, Matches 

    ' Uses late binding so no need to add references 
    Set regEx = CreateObject("VBScript.RegExp") 

    ' Change this to match your range where your values are 
    With Sheet2 
     Set rng = .Range(.Cells(1, 1), .Cells(8, 1)) 
    End With 
    ' Declare RegEx parameters 
    With regEx 
     .Pattern = "(?:\d*\.)?\d+" 
     .IgnoreCase = True 
     .Global = True 
    End With 
    ' Loop through each row 
    For Each c In rng 
     Set Matches = regEx.Execute(c) 
     If Matches.Count > 0 Then 
      ReDim arr(1 To Matches.Count) 
      k = 1 
      For Each Match In Matches 
       arr(k) = Match 
       k = k + 1 
      Next Match 
      ' Print back to worksheet 
      Range(c.Offset(0, 1), c.Offset(0, UBound(arr))) = arr 
     End If 
    Next c 

End Sub 

你必須決定如何處理返回的值的第一列,或等到有人認爲是更好地與正則表達式架起一個答案

+0

偉大的解決方案湯姆! – 2016-11-22 15:43:26

+0

它完美的工作,謝謝! – JohnSmith