Excel VBA复制,粘贴,插入variables行

我对VBA很新。 我试图从1工作簿的数据复制行,并将其插入到另一个。 源具有可变的行数。 目标只有10个可用行,之后是合并单元格。

我的目标是:对源行进行计数,从中减去10,结果将是在粘贴之前要在目标工作簿中插入的行数。

示例:32个源行(变化) – 10个目标行(固定)= 22行要插入到目标WB中,将合并的单元向下推。 如果结果<= 10,只需粘贴而不插入。

谢谢!

像这样的东西:

Sub Test() Dim sourceRange As Range, destinationRange As Range Dim rowsToInsert As Long With ThisWorkbook.Worksheets("Sheet1") Set sourceRange = .Range(.Range("A1"), .Cells(.Rows.Count, "A").End(xlUp)) End With Set destinationRange = Workbooks("SomeBook.xlsx").Worksheets("Sheet1").Range("A1") rowsToInsert = Application.Max(sourceRange.Rows.Count - 10, 0) If rowsToInsert > 0 Then destinationRange.Resize(rowsToInsert, 1).EntireRow.Insert sourceRange.EntireRow.Copy Destination:=destinationRange End Sub