【发布时间】:2021-10-29 20:54:08
【问题描述】:
我有一个 Excel 表,其中包含来自 A:N 的 50,000 多行数据。我有一个在 BackupData 工作表中有查询的主数据表。我目前复制该数据并作为值粘贴到备份工作表中。带有标题:
| ID | Vendor # | Name | Customer # | Customer | Invoice # | Date | Item# | Item Description | Qty | B/C | Lbs | Amt | Amt#2 |
|---|
我正在尝试遍历所有这些行并复制单元格 A:N 的范围,直到 A 列中的第一个值发生变化,即第一个不同的 ID #。
然后我需要将所选范围粘贴到新工作簿中。
基本上,我想做与整合相反的事情。
Sub inserting()
Dim wsBData, wsExport, wsCoverSht, wsBackup As Worksheet
Dim wbAllRebates, wbSingle As Workbook
Set wbAllRebates = ActiveWorkbook
Set wsBData = wbAllRebates.Sheets("BackupData")
Set wsBackup = wbAllRebates.Sheets("Backup")
Dim rID, rTopRow As Range
Dim i As Long
Dim Counter As Integer
i = 3
Set rTopRow = Rows(1)
Set rID = wsBackup.Range("A1")
wsBData.Cells.Copy
wsBackup.Cells.PasteSpecial Paste:=xlPasteValues
Counter = 0
LastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Do
If rID.Offset(i).Value <> rID.Offset(i - 1).Value Then
Rows(rID.Offset(i).Row).Insert shift:=xlDown
Call SubTotals(rID.Offset(i), rTopRow)
i = i + 1
Set rTopRow = Rows(rID.Offset(i).Row)
End If
Exit Do
Loop
MsgBox i
End Sub
Sub SubTotals(rID As Range, firstRow As Range)
rID.Value = "Total"
rID.Offset(, 9).Value = Application.WorksheetFunction.Sum(Range(firstRow.Cells(1, 10).Address & ":" & rID.Offset(-1, 1).Address))
End Sub
【问题讨论】: