|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
这两天花了不少时间更新代码,按原思路不好弄,标点符号太复杂了,只好再变通,多了几十行,经简单测试基本上通过,小问题会有,请详细测试,最怕是有删减。可能也会出现冲突情形,如中药名最后的字是汉字数字的或本身成数量形式的,请楼主自行预处理。文档内容组合情况千变万化,如不能一次性告知,别人是无法很难每次都满足要求的,还是请楼主自行修改代码,其实有的修改是比较简单的。不清楚可以交流,应该会有人清楚的。我也不是专业人士,代码仅作学习交流之用。- Private num$(99)
- Sub test4()
- 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)
- 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)
- info1(3, n) = info(2, j) - info(2, j - 1) + 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) - 1
- 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) Then
- If info(5, m) = num(k) And info(8, m) = Empty Then
- info2(i) = info2(i) & info(0, m) & "、"
- ElseIf info(5, m) = num(k) And info(8, m) <> Empty Then
- info2(i) = info2(i) & info(0, m) & "、"
- End If
- End If
- Next
- Next
- Next
- For m = 1 To Len(code) '处理判断为相同数量及单位的药材
- If info2(i) <> Empty Then
- With RegExp2
- .Global = True
- .Pattern = "([^、;。]+?)◆?各?([十九八七六五四三二一半]+" & Mid(code, m, 1) & "半?)、([^、。]+?)◆?各?\2([、,。]|$)"
- If InStr(info2(i), Mid(code, m, 1)) Then
- Do While .test(info2(i)) = True
- info2(i) = .Replace(info2(i), "$1◇$3◆各$2$4") '此处与原处理要求有所不同
- Loop
- .Pattern = "◇"
- info2(i) = .Replace(info2(i), "、")
- .Pattern = "◆"
- info2(i) = .Replace(info2(i), ",")
- End If
- .Pattern = "(,各([十九八七六五四三二一半]+" & Mid(code, m, 1) & ")[、,;。])(.*?\2)(?=[、,;。])"
- info2(i) = .Replace(info2(i), "、$3")
- 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) - 1
- Else
- .SetRange Selection.Start + data1(2, i) - 1, Selection.Start + data1(2, i) + data1(3, i) - 1
- 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
- Sub NumToCnNum()
- '生成百位内汉字小写数字
- Const a As String = "一二三四五六七八九"
- Dim i%
-
- For i = 1 To 99
- Select Case i
- Case Is >= 20
- If i Mod 10 > 0 Then
- num(i) = Mid(a, Int(i / 10), 1) & "十" & Mid(a, i Mod 10, 1)
- Else
- num(i) = Mid(a, Int(i / 10), 1) & "十"
- End If
- Case Is > 10
- num(i) = "十" & Mid(a, Int(i Mod 10), 1)
- Case Is < 10
- num(i) = Mid(a, i, 1)
- Case Else
- num(i) = "十"
- End Select
- Next
- num(0) = "半"
- End Sub
复制代码 |
评分
-
2
查看全部评分
-
|