|
我了一下第二个问题:“于是想到,若是增加些难度,替换为这样:”,只用了find ,如果用正则应该写起来没有这么麻烦!
Sub shishi()
Dim myFind$, oRange As Range, a, b, c, P As Range, H As Range
myFind = "[0-9]@、*[\((]*[\))]*^13标准答案:*[A-Z]^13"
Set oRange = ActiveDocument.Content
a = SplitRange(oRange, myFind)
For i = 0 To UBound(a)
If a(i) <> "" Then
b = SplitRange(a(i), "标准答案:")
For j = 0 To UBound(b)
If b(j) <> "" Then
b(j).Start = b(j).Start + 5: b(j).End = b(j).End - 1
t = Split(b(j), "、")
End If
Next
Set P = a(i).Duplicate
Do While P.Find.Execute("[A-Z]、[!^13A-Z]{1,}", , , 1)
If Not P.InRange(a(i)) Then Exit Do
For x = 0 To UBound(t)
If InStr(P, t(x)) Then
s = s & "、" & Mid(Trim(P), 3)
End If
Next
Loop
s = Mid(s, 2)
Set H = a(i).Duplicate
Do While H.Find.Execute("[\((]*[\))]", , , 1)
If Not H.InRange(a(i)) Then Exit Do
H.Start = H.Start + 1: H.End = H.End - 1: H = s
Loop
s = ""
End If
Next
oRange.Find.Execute "[A-Z]、*标准答案:*^13", , , 1, , , , , , "", 2
End Sub
Function SplitRange(myRng, myFind)
Dim lngStart&, lngEnd&, myStart&, n&, arr()
Dim myDoc As Document, P As Range
Set myDoc = ThisDocument: Set P = myRng.Duplicate
With P.Find
Do While .Execute(myFind, , , 1)
n = n + 1: If Not .Parent.InRange(myRng) Then Exit Do
With .Parent
lngStart = .Start: lngEnd = .End
If n > 1 Then
ReDim Preserve arr(n - 2)
Set arr(n - 2) = myDoc.Range(myStart, .Start)
End If
myStart = .Start: .SetRange lngEnd, lngEnd
End With
Loop
If n > 1 Then
ReDim Preserve arr(n - 1)
Set arr(n - 1) = myDoc.Range(myStart, myRng.End)
Else
ReDim arr(0)
Set arr(0) = myDoc.Range(myStart, myRng.End)
End If
End With
SplitRange = arr
End Function
|
|