|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub 药材按升斤排序()
- Dim arr, crr(), drr, err
- Dim i%, j%, k%, l%, index%, ShuJC%
- Dim str As String, s As String
- Dim brr() As String, ShuJurr
- str = "组成:附子(熟)10克、干姜6克、鸡子黄1个、花椒子13粒(捣碎)。人参6克、白术(炙)10克、茯苓10克、陈皮6克、甘草(炙)6克、五味子3克、半夏(洗,去滑)10克、生姜3片、红枣4枚(切开)。"
- arr = Split("克、枚、段、片、粒、个", "、")
- str = Replace(str, "组成:", "")
- str = Left(str, Len(str) - 1)
- str = Replace(str, "。", "、")
-
- brr = Split(str, "、")
- ReDim drr(LBound(brr) To UBound(brr))
- ReDim crr(LBound(brr) To UBound(brr), 1 To 3) '表格
- '正则表达式
- Dim reg As Object
- Dim Ma
-
- Set reg = CreateObject("VBScript.RegExp")
- For i = LBound(brr) To UBound(brr)
- With reg
- .Global = False
- .IgnoreCase = True '不区分大小写
- .Pattern = "\d{1,}" '正则表达式
- Set Ma = .Execute(brr(i)) '返回在文本中匹配的集合
- '拆分为表格
- crr(i, 1) = Left(brr(i), Ma(0).firstindex)
- crr(i, 2) = mid(brr(i), Ma(0).firstindex + 1, Ma(0).Length)
- crr(i, 3) = Right(brr(i), Len(brr(i)) - Ma(0).Length - Ma(0).firstindex)
- End With
- Next i
- 二维数组排序3 crr, 2
- i = 0
-
- '重新组合
- ReDim ShuJurr(LBound(crr) To UBound(crr))
- For i = LBound(crr) To UBound(crr)
- ShuJurr(i) = crr(i, 1) & crr(i, 2) & crr(i, 3)
- Next
-
- '"克、枚、段、片、粒、个"排序
- For i = LBound(arr) To UBound(arr)
- For j = LBound(ShuJurr) To UBound(ShuJurr)
- If InStr(ShuJurr(j), arr(i)) Then
- drr(k) = ShuJurr(j)
- k = k + 1
- End If
- Next
- Next
-
- '文字输出
- For i = LBound(drr) To UBound(drr)
- s = s & drr(i) & "、"
- Next
- End Sub
- Sub 二维数组排序3(ByRef arr() As Variant, WeiDu As Integer)
- Dim numRows As Integer, i As Integer, j As Integer, k As Integer
- Dim s As Single, s1 As Single
- numRows = UBound(arr) - LBound(arr) + 1
-
- Dim temp()
- ReDim temp(LBound(arr, 2) To UBound(arr, 2))
-
- For i = 0 To numRows - 1
- For j = 0 To numRows - 2
- s = arr(j, WeiDu)
- s1 = arr(j + 1, WeiDu)
- If s > s1 Then
- ' 交换位置
- For k = LBound(arr, 2) To UBound(arr, 2)
- temp(k) = arr(j, k)
- arr(j, k) = arr(j + 1, k)
- arr(j + 1, k) = temp(k)
- Next
- End If
- Next j
- Next i
- End Sub
复制代码 |
|