我有一个excel文件,其中有些单元格的括号内和括号外有多个文本。我想分裂牢房。例如,我有一些像这样的单元格
(some text in) parenthesis and (others outside)
我将拆分单元格,以使some text in
位于不同的单元格中,parenthesis and
也处于不同的单元格中,others outside
也处于不同的单元格中。到目前为止,我所得到的只是拆分括号中的内容。提前致谢。这是我的下面的代码
Sub StripCells()
Dim r As Range, i As Long
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "\(([^\)]+)\)"
For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
If .test(r.Value) Then
For i = 0 To .Execute(r.Value).Count - 1
r(, i + 2).Value = "'" & .Execute(r.Value)(i).submatches(0)
Next
End If
Next
End With
End Sub
答案 0 :(得分:1)
如果您所有的单元格都以“ (”开头,则:
Sub fracture()
Dim r As Range, a, arr, i As Long
For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
arr = Split(Replace(r.Value, "(", ")"), ")")
i = 0
For Each a In arr
If i <> 0 Then
r.Offset(0, i).Value = a
End If
i = i + 1
Next a
Next r
End Sub
答案 1 :(得分:0)
我发现了一种有效的模式。 (我作弊,并使用Replace()
,但似乎可以解决问题):
Sub StripCells()
Dim r As Range, i As Long
With CreateObject("VBScript.RegExp")
.Global = True
'.Pattern = "\(([^\)]+)\)"
.Pattern = "((\(([^\)]+)\))|[\w+ ]+)"
For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
If .test(r.Value) Then
For i = 0 To .Execute(r.Value).Count - 1
r(, i + 2).Value = "'" & Replace(Replace(.Execute(r.Value)(i).submatches(0), ")", ""), "(", "")
Next
End If
Next
End With
End Sub