簡體   English   中英

Excel VBA復制,粘貼,插入變量行

[英]Excel VBA Copy, Paste, Insert variable rows

我是VBA的新手。 我正在嘗試從1個工作簿復制包含數據的行,並將其插入另一個工作簿。 源具有可變的行數。 該目標只有10個可用行,后跟合並的單元格。

我的目標是:計算源行,從中減去10,結果將是粘貼之前要在目標工作簿上插入的行數。

示例:32個源行(變量)-10個目標行(固定)=要插入目標WB的22行,將合並的單元格向下推。 如果結果<= 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM