|
|
- Sub SplitInvoicesWithArray()
- Application.ScreenUpdating = False
- Application.Calculation = xlCalculationManual
-
- Dim ws As Worksheet
- Set ws = Sheet1
-
- Dim originData As Variant
- Dim resultData() As Variant
- Dim rowCounter As Long
- Dim i As Long, j As Long, n As Long
- ' 读取原始数据到数组
- With ws
- Dim lastRow As Long
- lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
- originData = .Range("A2:E" & lastRow).Value ' 排除标题
- End With
-
- ' 计算最大可能行数
- ReDim resultData(1 To UBound(originData) * 10, 1 To 5) ' 假设每个单元格最多10个发票号
-
-
-
- ' 主处理循环
- rowCounter = 0
- For i = 1 To UBound(originData, 1)
- Dim invoices() As String
- invoices = Split(Trim(originData(i, 4)), ",") ' 拆分D列
- n = 0
- ' 写入拆分数据
- For Each inv In invoices
- If Len(Trim(inv)) > 0 Then
- rowCounter = rowCounter + 1
- n = n + 1
- ' 复制基础数据
- If n = 1 Then
- For j = 1 To 3
- resultData(rowCounter, j) = originData(i, j)
- Next j
-
- ' 金额复制
- resultData(rowCounter, 5) = originData(i, 5)
-
- End If
- ' 发票号处理
- resultData(rowCounter, 4) = Trim(inv)
- End If
- Next inv
- Next i
-
- ' 清除原始数据
- ws.Range("i:m").ClearContents
- ' 处理标题
- ws.Range("A1:E1").Copy ws.Range("i1")
- ' 写入处理结果
- If rowCounter > 0 Then
- ws.Range("i2").Resize(rowCounter, 5).Value = resultData
- End If
-
- Application.Calculation = xlCalculationAutomatic
- Application.ScreenUpdating = True
- MsgBox "处理完成,共生成 " & rowCounter & " 行数据", vbInformation
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|