简体   繁体   English

在工作表 3 中复制范围 A:AM 而不是整行并粘贴到工作表 1 (A1) 中的一个下方

[英]Copy range A:AM in sheet 3 instead of entire row and paste in sheet1 (A1) one below the other

How do I make a change in the code so that it copies only range A:AM in sheet3 till the last row in column D, instead of copying the entire row and paste it in Sheet1 (A1) one below the other?如何更改代码,使其仅复制 sheet3 中的范围 A:AM 直到 D 列的最后一行,而不是复制整行并将其粘贴到 Sheet1 (A1) 中的一个下方?

Option Explicit 
Public Sub ABC()
    Dim LastRow As Long
    LastRow = Sheets(3).Cells(Rows.Count, "d").End(xlUp).Row

    Dim iRow As Long
    For iRow = 7 To LastRow
        If Application.WorksheetFunction.CountA(Sheets(3).Range("J" & iRow  & ":AM" & iRow)) <> 0 Then
            Sheets(3).Rows(iRow).Copy Destination:=Sheets(1).Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
        End If
    Next iRow
End Sub

Like this:像这样:

Sheets(3).Range("A" & iRow & ":AM" & iRow).Copy

or或者

Sheets(3).Cells(irow, "A").Resize(1, 39).Copy

or或者

Sheets(3).Range("A:AM").Rows(iRow).Copy

If your goal is to replace the loop with a entire range copy/paste all at once then:如果您的目标是一次用整个范围复制/粘贴替换循环,那么:

Create a variable to store the last row index based off column D and then just create your dynamic range to copy accordingly创建一个变量来存储基于列D的最后一行索引,然后创建动态范围以进行相应的复制


Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim lr As Long

lr = ws.Range("D" & ws.Rows.Count).End(xlUp).Row          '<-- Find Last Row
ws.Range("A1:AM" & lr).Copy                               '<-- Copy
Sheets("Sheet2").Range("A1").PasteSpecial xlPasteValues   '<-- Paste

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM