Hiko.Blog Excel VBA活用術

「Excel VBAで仕事を効率化!初心者でもできる自動化のコツ」

MENU

TextBoxから特定の文字抽出 修正版

Sub TextBoxから特定の文字抽出 修正版()
    Dim shp As Shape
    Dim fullText As String
    Dim lines As Variant
    Dim ws As Worksheet
    Dim regex1 As Object, regex2 As Object, regex3 As Object, regex4 As Object
    Dim matches1 As Object, matches2 As Object, matches3 As Object, matches4 As Object
    Dim i As Long, rowOut As Long, startLine As Long
    Dim line As String
    Dim foundStart As Boolean
    Dim outputArr() As Variant

    Set ws = ThisWorkbook.Sheets("Sheet1")
    Set shp = ws.Shapes("TextBox 1")

    fullText = Replace(shp.TextFrame.Characters.Text, " ", " ") ' 全角スペースを半角に
    fullText = Application.WorksheetFunction.Trim(fullText)

    If InStr(fullText, vbCrLf) > 0 Then
        lines = Split(fullText, vbCrLf)
    ElseIf InStr(fullText, vbLf) > 0 Then
        lines = Split(fullText, vbLf)
    Else
        lines = Split(fullText, ".")
    End If

    ' 正規表現初期化
    Set regex1 = CreateObject("VBScript.RegExp")
    With regex1
        .Pattern = "[A-Za-z0-9]{3}-\d{4}-\d{3}" ' パターン1
        .Global = False
        .IgnoreCase = False
    End With

    Set regex2 = CreateObject("VBScript.RegExp")
    With regex2
        .Pattern = "[A-Za-z0-9]{13}" ' パターン2
        .Global = False
        .IgnoreCase = False
    End With

    Set regex3 = CreateObject("VBScript.RegExp")
    With regex3
        .Pattern = "(^|[^-\d])(\d{3}|00T)([^-\d]|$)" ' パターン3: 3桁または "00T" を含むコード
        .Global = False
        .IgnoreCase = False
    End With

    ' PCS前の数値を抽出するパターン
    Set regex4 = CreateObject("VBScript.RegExp")
    With regex4
        .Pattern = "(\d+)\s*PCS" ' PCS前の数値
        .Global = False
        .IgnoreCase = True ' 小文字でもマッチ
    End With

    ' パターン1にマッチする最初の行を探す
    foundStart = False
    startLine = 0
    For i = LBound(lines) To UBound(lines)
        line = Trim(lines(i))
        If regex1.Test(line) Then
            startLine = i
            foundStart = True
            Exit For
        End If
    Next i

    If Not foundStart Then
        MsgBox "パターン1が見つかりませんでした。処理を中止します。", vbExclamation
        Exit Sub
    End If

    rowOut = 0
    ReDim outputArr(1 To UBound(lines) - startLine + 1, 1 To 5) ' A~E列分

    ' 対象行の処理
    For i = startLine To UBound(lines)
        line = Trim(lines(i))
        
        ' パターン1に一致しない行はスキップ
        If Not regex1.Test(line) Then GoTo NextIteration

        ' パターン1
        Set matches1 = regex1.Execute(line)
        If matches1.Count > 0 Then
            outputArr(rowOut + 1, 1) = matches1(0).Value
        End If

        ' パターン3(3桁または00T)
        Set matches3 = regex3.Execute(line)
        If matches3.Count > 0 Then
            outputArr(rowOut + 1, 2) = "’" & matches3(0).SubMatches(1)
        End If

        ' パターン2
        Set matches2 = regex2.Execute(line)
        If matches2.Count > 0 Then
            outputArr(rowOut + 1, 3) = matches2(0).Value
        End If

        ' パターン4(PCS前の数値)
        Set matches4 = regex4.Execute(line)
        If matches4.Count > 0 Then
            outputArr(rowOut + 1, 4) = matches4(0).SubMatches(0) ' PCS前の数値
        End If

        rowOut = rowOut + 1

NextIteration:
    Next i

    ' 結果をA2セルから書き出し
    If rowOut > 0 Then
        ws.Range(ws.Cells(2, "A"), ws.Cells(1 + rowOut, "E")).Value = _
            Application.Index(outputArr, Evaluate("ROW(1:" & rowOut & ")"), Evaluate("COLUMN(1:5)"))
    Else
        MsgBox "出力対象がありませんでした。", vbInformation
    End If

    MsgBox "抽出完了しました!"
End Sub