|
- Sub InfoList()
- Dim vData As Variant, nRow As Long, nCol As Long, nI As Long, nJ As Long
- Dim sConnect As String, dicConnectChar As Object
- Dim nCodeCol As Long, nMemoCol As Long
- Dim sMemo As String, sCode As String
- Dim vDivide As Variant, sDivide As String, vConnect As Variant, sNum(1) As String, nLen As Long, nNum As Long, sSerial As String
- Dim vFill As Variant, nFill As Long
-
- Set dicConnectChar = CreateObject("Scripting.Dictionary")
- With Sheet2
- nRow = .Cells(.Rows.Count, 1).End(xlUp).Row
- vData = .[A1:B1].Resize(nRow).Value
- For nRow = 3 To UBound(vData)
- If vData(nRow, 2) = "-" Then dicConnectChar(vData(nRow, 1)) = 0 '记录需要替换的相连符号
- Next
- sConnect = "" & Join(dicConnectChar.Keys(), "|") '组建正则查找字符对象
-
- nRow = .Cells(.Rows.Count, 5).End(xlUp).Row
- vData = .[D1:E1].Resize(nRow).Value
- nCodeCol = vData(3, 2) '代码列号
- nMemoCol = vData(5, 2) '备注列号
- End With
-
- With Sheet1
- nRow = Application.WorksheetFunction.Min(.Cells(.Rows.Count, nCodeCol).End(xlUp).Row, .Cells(.Rows.Count, nMemoCol).End(xlUp).Row) '查找代码列、备注列的最小行数
- nCol = Application.WorksheetFunction.Max(nCodeCol, nMemoCol) '查找代码列号、备注列号之中的最大数
- vData = .[A1].Resize(nRow, nCol).Value
- ReDim vFill(1 To 2, 0)
- With CreateObject("Vbscript.RegExp")
- .Global = True
- .IgnoreCase = True
- For nRow = 2 To UBound(vData)
- sCode = vData(nRow, nCodeCol)
- sMemo = vData(nRow, nMemoCol)
- .Pattern = "[" & sConnect & "]+"
- If .Test(sMemo) Then sMemo = .Replace(sMemo, "-") '替换相连符号为-号
- .Pattern = "[^\-\w\d]+"
- If .Test(sMemo) Then sMemo = .Replace(sMemo, ",") '替换非-号、非字母、非数字的符号为,号
- vDivide = Split(sMemo, ",") '分离各个分开的段
- For nI = LBound(vDivide) To UBound(vDivide)
- sDivide = vDivide(nI)
- vConnect = Split(sDivide, "-")
- .Pattern = "\d+$"
- sNum(0) = .Execute(vConnect(0))(0) '取最小序号部分
- nLen = Len(sNum(0))
- sSerial = Left(vConnect(0), Len(vConnect(0)) - nLen) '取非序号部分
- If UBound(vConnect) = 1 Then '说明有相连
- sNum(1) = .Execute(vConnect(1))(0) '取最大序号部分
- Else
- sNum(1) = sNum(0)
- End If
- For nNum = Val(sNum(0)) To Val(sNum(1))
- nFill = nFill + 1
- ReDim Preserve vFill(1 To 2, 1 To nFill)
- vFill(1, nFill) = sSerial & Right("00000" & nNum, nLen)
- vFill(2, nFill) = sCode
- Next
- Next
- Next
- End With
- .[E:F].ClearContents
- .[E1:F1].Resize(nFill) = Application.WorksheetFunction.Transpose(vFill)
- End With
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|