2013-07-11 35 views
-1

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

例如:

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

我只想要得到的值346.32

我这些数据像这样为A1:A20我怎样才能使用VBA全部更换?

+1

一个代码,或者手动?有或没有VBA?用VBA解释更多 – gpalex

+0

对不起.. –

回答

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 
+0

嗨amadeus ..谢谢你的回复..但我应该把这个代码放在哪里? –

+0

我试图运行脚本,但它没有迭代..它只改变第一个单元格A1 A1 –

+0

转到Visual Basic编辑器(按ALT + F11或通过开发人员选项卡),然后单击您想要的工作表替换单元格。将代码粘贴到窗口并执行宏(按F5) –

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 
+0

如果我在A1-A20上有这些数据怎么办? –

+0

@ArgeMotoharu通过A1:A20循环并重复将内容拆分成数组并逐个处理每个单元格的过程。 –

相关问题