|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 sylun 于 2022-9-8 22:25 编辑
更新后的代码1,请测试- Private num(0 To 999) As String
- Sub test5()
- Dim i%, j%, k%, m%, n%, r%, s%, code$, data$, data2$
- Dim info(), info1&(), info2$(), data1()
- Dim RegExp As Object
- Dim Match As Object
- Dim RegExp2 As Object
- Dim Match2 As Object
-
- Call NumToCnNum
- data = IIf(Selection.Type = wdSelectionIP, ActiveDocument.Content.Text, Selection.Text)
- code = "升斤两克钱分厘枚粒片" '数量单位据此先后排序
- Set RegExp = CreateObject("VBScript.RegExp")
- Set RegExp2 = CreateObject("VBScript.RegExp")
- With RegExp
- .Global = True
- .Pattern = "(([^。;:、\r]*[^升斤两克钱分厘枚粒片半、]、)*?[^。;:、\r]+(([^)]+))?)([十九八七六五四三二一半百]+)([①-⑩]?)([升斤两克钱分厘枚粒片])([①-⑩半]?(([^)]+))?)(?=[。,、])"
- If .test(data) = False Then
- MsgBox "找不到匹配,程序退出!", vbCritical
- Exit Sub
- End If
- For Each Match In .Execute(data) '获取匹配方剂各药材配伍文本相关定位参数
- ReDim Preserve info(8, i)
- With Match
- info(0, i) = .Value
- info(1, i) = .Length
- info(2, i) = .firstindex
- info(3, i) = info(1, i) + info(2, i) + 1
- info(4, i) = .submatches(0)
- info(5, i) = .submatches(3)
- info(6, i) = .submatches(4)
- info(7, i) = .submatches(5)
- info(8, i) = .submatches(6)
- End With
- i = i + 1
- Next
- End With
-
- If i > 1 Then
- r = 0
- For j = 1 To i - 1
- If info(2, j) <> info(2, j - 1) + info(1, j - 1) + 1 Then '对目标文本按匹配配伍与相邻字符串依次进行分组
- ReDim Preserve info1(4, n)
- If j - r > 1 Then '有两个以上匹配相连时合并为一组
- info1(0, n) = r
- info1(1, n) = j - 1
- info1(2, n) = info(2, r) + 1
- info1(3, n) = info(2, j - 1) - info(2, r) + info(1, j - 1) + 1
- info1(4, n) = 1
- r = j
- n = n + 1
- ReDim Preserve info1(4, n)
- info1(0, n) = r
- info1(1, n) = j - 1
- info1(2, n) = info(3, j - 1)
- info1(3, n) = info(2, j) - info(3, j - 1) + 1
- info1(4, n) = 0
- Else
- info1(0, n) = r
- info1(1, n) = j - r
- info1(2, n) = info(3, j - 1) + 1
- info1(3, n) = info(2, j) - info(3, j - 1)
- info1(4, n) = 0
- If n > 0 Then
- If info1(4, n - 1) = 0 Then
- info1(2, n) = info(2, j - 1) + 1
- info1(3, n) = info(2, j) - info(2, j - 1)
- End If
- End If
- r = j
- End If
- n = n + 1
- End If
- Next
- ReDim Preserve info1(4, n) '目标文本前部没有匹配的字符串
- info1(0, n) = r
- info1(1, n) = j - 1
- info1(2, n) = info(2, r) + 1
- info1(3, n) = info(3, j - 1) - info1(2, n) + 1
- info1(4, n) = 1
- If Len(data) > info1(2, n) + info1(3, n) Then '目标文本末尾没有匹配的字符串
- n = n + 1
- ReDim Preserve info1(4, n)
- info1(0, n) = r
- info1(1, n) = j - 1
- info1(2, n) = info(2, j - 1) + info(1, j - 1) + 1
- info1(3, n) = Len(data) - info1(2, n)
- End If
-
- ReDim data1(6, UBound(info1, 2))
- For k = 0 To UBound(data1, 2)
- data1(0, k) = info1(0, k)
- data1(1, k) = info1(1, k)
- data1(2, k) = info1(2, k)
- data1(3, k) = info1(3, k)
- data1(4, k) = Mid(data, info1(2, k), info1(3, k))
- data1(5, k) = info1(4, k)
- Next
- ReDim info2(UBound(data1, 2))
- For i = 0 To UBound(data1, 2)
- If data1(5, i) = 1 Then
- s = s + 1
- For j = 1 To Len(code) '处理方剂药材配伍文本
- For k = 99 To 0 Step -1 '对提取的药材以剂量大小进行排序
- For m = data1(0, i) To data1(1, i)
- If info(7, m) = Mid(code, j, 1) And info(5, m) = num(k) Then info2(i) = info2(i) & info(0, m) & "、"
- Next
- Next
- Next
- For m = 1 To Len(code) '处理判断为相同数量及单位的药材
- If info2(i) <> Empty Then
- With RegExp2
- .Global = True
- .Pattern = "([^、;。]+?)◆?各?([十九八七六五四三二一半]+)([①-⑩]?)(" & Mid(code, m, 1) & "半?)([①-⑩]?(([^)]+))?)、([^、。]+?)◆?各?\2\4([①-⑩]?([^)]+))?([、,。]|$)"
- If InStr(info2(i), Mid(code, m, 1)) Then
- Do While .test(info2(i)) = True
- info2(i) = .Replace(info2(i), "$1$3$5◇$7$8◆各$2$4$9")
- Loop
- .Pattern = "[◇,、]+"
- info2(i) = .Replace(info2(i), "、")
- .Pattern = "[◆,]+"
- info2(i) = .Replace(info2(i), ",")
- End If
- .Pattern = ",?(,各[十九八七六五四三二一半]+" & Mid(code, m, 1) & ")[、,;。](.+?\1)(?=[、,;。])"
- info2(i) = .Replace(info2(i), "、$2")
- End With
- End If
- Next m
- data1(6, i) = "△" & Left(info2(i), Len(info2(i)) - 1) & "▲"
- Else
- data1(6, i) = data1(4, i)
- End If
- Next
- End If
- data2 = Left(data, data1(2, 0) - 1)
- For i = 0 To UBound(data1, 2)
- data2 = data2 & data1(6, i)
- Next
-
- With IIf(Selection.Type = wdSelectionIP, ActiveDocument.Content, Selection.Range)
- For i = 0 To UBound(data1, 2)
- If data1(5, i) = 1 Then
- If Selection.Type = wdSelectionIP Then
- .SetRange data1(2, i) - 1, data1(2, i) + data1(3, i) - 2
- Else
- .SetRange Selection.Start + data1(2, i) - 1, Selection.Start + data1(2, i) + data1(3, i) - 2
- End If
- .HighlightColorIndex = wdYellow '在原文档突出显示匹配文本
- End If
- Next
- End With
-
- With Documents.Add(ActiveDocument.FullName).Content.Find
- .Parent.Text = data2 '将处理结果输出到以原文档为模板的新文档
- .Replacement.Highlight = True
- .Execute "△*▲", MatchWildcards:=True, replacewith:="^&", Replace:=wdReplaceAll
- End With
- MsgBox "共处理了" & s & "条,处理条目见原文档标记、结果文本见新文档突出显示部分。"
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|