|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub test()
- Dim r%, i%
- Dim arr, brr, crr()
- Dim reg1 As New RegExp
- Dim reg2 As New RegExp
- Dim reg3 As New RegExp
- With reg1
- .Global = False
- .Pattern = "^\d{18}"
- End With
- With reg2
- .Global = False
- .Pattern = "^\d{18}\s*(.+?)(\d{4}-\d{1,2}-\d{1,2}\s*至\s*\d{4}/\d{1,2}/\d{1,2})\s*(\d{4}-\d{1,2}-\d{1,2})\s*([\d\.,]+)"
- End With
- With reg3
- .Global = False
- .Pattern = "金额合计.+?([\d.]+)"
- End With
- With Worksheets("sheet1")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a1:a" & r)
- ReDim brr(1 To UBound(arr), 1 To 1)
- For i = 1 To UBound(arr)
- If Not arr(i, 1) Like "金额合计*" Then
- If reg1.test(arr(i, 1)) Then
- m = m + 1
- End If
- brr(m, 1) = brr(m, 1) & arr(i, 1)
- Else
- Set mh = reg3.Execute(arr(i, 1))
- If mh.Count > 0 Then
- hj = mh(0).SubMatches(0)
- End If
- End If
- Next
- ReDim crr(1 To m + 1, 1 To 4)
- n = 0
- For i = 1 To m
- Set mh = reg2.Execute(brr(i, 1))
- If mh.Count > 0 Then
- n = n + 1
- crr(n, 1) = mh(0).SubMatches(0)
- crr(n, 2) = Replace(mh(0).SubMatches(1), Space(1), Empty)
- crr(n, 3) = mh(0).SubMatches(2)
- crr(n, 4) = mh(0).SubMatches(3)
- End If
- Next
- n = n + 1
- crr(n, 1) = "金额合计"
- crr(n, 4) = hj
- If n > 0 Then
- .Range("d1").Resize(n, UBound(crr, 2)) = crr
- End If
- End With
- End Sub
复制代码 |
|