从excel中提取特定格式

时间:2012-06-22 14:43:30

标签: excel vba

我完全没有的经验,而且依赖于古老的Java知识

我有以下格式的数据,我想提取001-222-170组件(从第1行等)

TEXT,TEXT,TEXT,001-222-170,TEXT    
LINER,612-942-001,TEXT    
TEXT,TEXT17,612-551-001,TEXT   
SHOE,435-687-204,TEXT    
TEXT,ASSY,O.B,632-005-202,TEXT    
TEXT,TEXT,I.B,632-012-202,TEXT

经过广泛的研究,我知道你必须设置.pattern认可。我最近的事情是

Sub RegEx()
    Dim RegEx As Object
    Dim strTest As String
    Dim valid As Boolean
    Dim Matches As Object
    Dim i As Integer

    Set RegEx = CreateObject("VBScript.RegExp")
    'What I happen to be looking for    
    RegEx.Pattern = "MT\d{6}V\d"

    For i = 2 To 115
        Range("B" & i).Activate
        strTest = ActiveCell.Text
        valid = RegEx.test(strTest)
        If valid = True Then
            Set Matches = RegEx.Execute(strTest)
            Range("C" & i).Value = CStr(Matches(0))
        Else
            Range("C" & i).Value = "#N/A#"
        End If
    Next

    Set RegEx = Nothing
End Sub

但它仍然没有做到这一点,我知道有一个更短的方法来做到这一点。我只是不知道如何格式化图案线。像("(\d)"-"(\d)"-"(\d)")

这样的东西

5 个答案:

答案 0 :(得分:2)

RegEx.Pattern = "\d{3}\-\d{3}\-\d{3}"

答案 1 :(得分:1)

以下代码将RegExp UDF转储到C2:C115一次性(无循环)分别在B2:b115上运行

你的正则表达式可以缩短为 (\d{3}\-){2}\d{3}"

Sub DumpReg()
    Range("C2:115").FormulaR1C1 = "=EXTRACT1(RC[-1])"
End Sub

Function Extract1(strIn As String) As String
    Dim objRegex As Object
    Dim objRegMC As Object
    Set objRegex = CreateObject("vbscript.regexp")
    With objRegex
        .Pattern = "(\d{3}\-){2}\d{3}"
        If .test(strIn) Then
            Set objRegMC = .Execute(strIn)
            Extract1 = objRegMC(0)
        Else
            Extract1 = "#N/A#"
        End If
    End With
End Function

答案 2 :(得分:0)

如果数据始终为您需要的XXX-XXX-XXX格式,您可以在不使用VBA或RegEx的电子表格上进行写入。

Example

更新VBA解决方案

这里有一个更简单的VBA解决方案,没有RegExp。

N.B。 - 此解决方案假设除了XXX-XX-XXX模式之外,单元格文本模式中不会有任何其他“ - ”实例。

Sub dude()

Dim strTest As String

For i = 2 To 115

    strTest = Range("A" & i).Text

    If InStr(1, strText, "-") > 0 Then
        Range("C" & i) = Mid(strTest, InStr(1, strText, "-") - 3, 11)
    Else
        Range("C" & i) = "#N/A#"
    End If

Next

End Sub

答案 3 :(得分:0)

您可以使用的模式是:\d+-\d+-\d+\d+只表示一个或多个小数。 该子可以写成:

Sub RegEx()
    Dim RegEx As Object
    Dim strTest As String
    Dim valid As Boolean
    Dim Matches As Object
    Dim i As Integer

    Set RegEx = CreateObject("VBScript.RegExp")

    RegEx.Pattern = "\d+-\d+-\d+"

    For i = 2 To 115
        strTest = Range("B" & i).Text
        Set Matches = RegEx.Execute(strTest)
        If Matches.Count > 0 Then
            Cells(i, "C").Value = CStr(Matches(0))
        Else
            Cells(i, "C").Value = "#N/A#"
        End If
    Next

    Set RegEx = Nothing
End Sub

答案 4 :(得分:0)

如果你的格式是相同的,并且你想要一个VBA解决方案,那么Regex就是这种东西的矫枉过正。这是使用SPLIT()

的简单方法
Sub Sample()
    Dim MyAr() As String, strSample As String

    strSample = "TEXT,TEXT,TEXT,001-222-170,TEXT"
    'strSample = "LINER,612-942-001,TEXT"

    MyAr = Split(strSample, ",")

    Debug.Print MyAr(UBound(MyAr) - 1)
End Sub
相关问题