|
- Sub 替换()
- Dim arr
- Set d = CreateObject("scripting.dictionary") '正向
- Set dr = CreateObject("scripting.dictionary") '反向
- arr = Range("d2:e" & [e65536].End(3).Row)
- For i = 1 To UBound(arr)
- xrr = Split(Trim(arr(i, 2)), " ")
- y = ""
- For k = UBound(xrr) To 0 Step -1
- y = y & " " & xrr(k)
- Next
- x = Trim(arr(i, 1))
- d(x) = Trim(arr(i, 2))
- dr(x) = Mid(y, 2)
- Next
-
- arr = Range("a1:c" & [a65536].End(3).Row)
- With CreateObject("vbscript.regexp")
- .Global = True
- For i = 2 To UBound(arr)
- x = arr(i, 2)
- a = Split(x, " ")
- For j = 0 To UBound(a)
- s = Trim(a(j))
- If d.exists(s) Then
- ks = a(j - 1): js = a(j + 1)
- .Pattern = ks & "( .+ )" & js
- If .test(d(s)) Then
- Set mh = .Execute(d(s))
- fx = "正向"
- Else
- Set mh = .Execute(dr(s)) '如果正向没有,反向查找一遍
- fx = "反向"
- End If
- If mh.Count = 0 Then
- arr(i, 3) = "未替换" 'x
- Else
- xstr = Trim(mh(0).submatches(0))
- arr(i, 3) = s & fx & ":" & Replace(x, s, xstr)
- Exit For
- End If
- End If
- Next
- Next
- End With
- Range("a1:c" & [a65536].End(3).Row) = arr
- End Sub
复制代码 |
|