아래 코드는 셀에 문자열을 찾고 그 앞에 오는 단어와 단어를 모두 추출합니다.셀 배열의 문자열 추출
검색 단어가 "(안녕하세요)"로 남겨지면 코드가 작동하지만 "안녕하세요"로 만들면 코드에서 찾을 수 없습니다. "hello"와 "hey"사이의 공백을 가정하지만 그 변경 방법을 잘 모릅니다.
Dim c As Range, v As String, arr, x As Long, e
Dim d As Range
Set d = WorkSheets("Sheet1").Range("F1") '<<results start here
For Each c In ActiveSheet.Range("D1:D10")
v = Trim(c.Value)
If Len(v) > 0 Then
'normalize other separators to spaces
v = Replace(v, vbLf, " ")
'remove double spaces
Do While InStr(v, " ") > 0
v = Replace(v, " ", " ")
Loop
'split to array
arr = Split(v, " ")
For x = LBound(arr) To UBound(arr)
e = arr(x)
'see if array element is a word of interest
If Not IsError(Application.Match(LCase(e), Array("(hello hey)"), 0)) Then
If x > LBound(arr) Then
d.Value = arr(x - 1) & " " & e 'prepend previous word
Else
d.Value = "??? " & e 'no previous word
End If
Set d = d.Offset(1, 0)
End If
Next x
End If
Next c
End Sub
"(안녕하세요 헤이)"는 공백을 포함하므로 절대 일치하지 않으며 모든 셀 값을 공백으로 나눕니다 (따라서'arr'의 값에는 공백이 포함되지 않습니다). –
@TimWilliams 괄호 내부를 단일 vaule로 처리 할 수있는 방법이 있습니까? – johndoe253