Excel 셀에서 여러 하위 문자열 추출

Excel 셀에서 여러 하위 문자열 추출

각 셀에 다음과 같은 항목이 0개 또는 하나 이상 포함될 수 있는 끔찍한 형식의 열이 있습니다(여기에는 두 개가 포함되어 있습니다).

ACTI-U-9754 - Some description MDCF-U-9791 - Some other description

가급적 수식을 사용하여 11개의 문자열을 별도의 열로 추출해야 합니다. 위 셀의 경우 다음과 같아야 합니다.

ACTI-U-9754
MDCF-U-9791

이 특정 시나리오를 다루는 예제를 찾지 못했습니다.

답변1

간단한 수식 방법이 생각나지 않아서 안타깝지만, 유용할 경우를 대비해 RegEx를 사용하는 VBA 방법이 있습니다. RegEx 패턴은 코드가 항상 동일하다고 가정합니다. 4 letters - 1 letter - 4 digits물론 필요에 따라 수정할 수 있습니다. 문자와 숫자의 가정이 올바르지 않지만 형식이 항상 4-1-4인 경우 .{4}\-.\-.{4}대신 다음을 사용할 수 있습니다.

여기에 이미지 설명을 입력하세요

Sub GetCodes()
    Dim strPattern: strPattern = "\w{4}\-\w\-\d{4}"   'Pattern to match
    Dim colNumber: colNumber = 1                        'Column number containing strings (In this case, 1, for column A)
    Dim rowCount: rowCount = 1                          'Row number to start from
    Range("B1").Select                                  'Cell to start new column from

    'Create a new RegEx engine instance
    Dim rgx: Set rgx = CreateObject("vbscript.regexp")

    'Set out RegEx instance to allow Global (More than 1 result per text), MultiLine (Incase there are any carriage returns in the cell), IgnoreCase (Allow both upper and lowercase, which isn't needed with \w but included to be sure) and Pattern, the patter defined above.
    With rgx
        .Global = True
        .MultiLine = True
        .IgnoreCase = True
        .Pattern = strPattern
    End With

    'Begin a loop that ends once we hit an empty cell
    Do
        'Get all our RegEx matches and store them in rgxMatches
        Dim rgxMatches: Set rgxMatches = rgx.Execute(Cells(rowCount, colNumber).Value)
        Dim rgxMatch
        'Loop through our matches
        For Each rgxMatch In rgxMatches
            'Write the match into the active cell
            ActiveCell.Value = rgxMatch.Value
            'Go down one row, ready to write the next cell if there is one
            ActiveCell.Offset(1, 0).Select
        Next

        'Increment our row count so the next loop uses the next row
        rowCount = rowCount + 1
    Loop Until IsEmpty(Cells(rowCount, colNumber))

    'Clean up after
    Set rgx = Nothing
    Set rgxMatches = Nothing
End Sub

여기에 이미지 설명을 입력하세요

관련 정보