|
单条正则也可以处理,但比较复杂。
如果对正则感兴趣,可以参考我总结的“万能正则表达式”公式,见以下帖子的第23楼:[第127期]替换特定规则范围内的字符[已总结评分]
https://club.excelhome.net/thread-1506418-1-1.html
(出处: ExcelHome技术论坛)
- Sub test()
- Dim Reg, ma, mas
- r = Cells(Rows.Count, 1).End(xlUp).Row
- arr = Cells(1, 1).Resize(r, 2)
- ReDim brr(1 To 3 * r, 1 To 2)
- Set Reg = CreateObject("VBScript.RegExp")
- With Reg
- .Global = True
- .Pattern = "(?:[\d\*x X×]+(?:预打包|[\((][新券])|(?:[\*x X×-]|新\d+\)|预打包)\d+|.*?)+(?:(\b\d{4}\b)(?=[^\d新]*(?:(?:\+\d{4})*\))?[\*x X×](\d+))?|$)"
- For i = 2 To r
- s = .Replace(StrConv(arr(i, 1), vbNarrow), "$1 $2 ") '正则替换,只保留条码和数量,放在2个“捕获分组”内,以空格符相间隔。
- If Len(s) > 3 Then
- ss = Split(s, " ")
- For j = 0 To UBound(ss) - 1 Step 2
- If Len(ss(j)) > 0 Then '如果条码非空
- n = n + 1 '则增加1项
- brr(n, 1) = ss(j)
- If j = UBound(ss) Then brr(n, 2) = 1 Else If ss(j + 1) = "" Then brr(n, 2) = 1 Else brr(n, 2) = ss(j + 1) '如果原数量被省略,则记为1
- End If
- Next
- End If
- Next
- End With
- Range("B2").Resize(n).NumberFormat = "@"
- Range("B2").Resize(n, 2) = brr
- End Sub
复制代码
电商编码转为瓶数.rar
(20.75 KB, 下载次数: 2)
|
|