如何在VBA中将列从一个工作表粘贴到另一个工作表?

时间:2014-03-03 13:39:48

标签: excel vba excel-vba

我在一张纸上有一行未知行数,我想将其复制并粘贴到另一张纸上。由于行数未知,我将其定义为变量:

Sub Official()
Dim lastrow As Long
Dim LastCol As Long
Set currentsheet = ActiveWorkbook.Sheets(1)

LastRow = Range("A65536").End(xlUp).Row
LastCol = Range("A1").End(xlToRight).Column

Sheets("Type_1").Range("D8" & "D" & LastRow).Copy
Sheets(1).Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=True
End Sub

我收到这个宏的错误,也许有人可以帮助我?

1 个答案:

答案 0 :(得分:0)

你可以尝试:

Sub Official()
Dim lastrow As Long
Dim LastCol As Long
Dim srcLastRow As Long

    Set currentsheet = ActiveWorkbook.Sheets(1)

    ' handle Office 2007+ with more than 65536 rows...
    lastrow = Range("A" & currentsheet.Rows.Count).End(xlUp).Row
    LastCol = Range("A1").End(xlToRight).Column

    ' find out how many rows there are in the source sheet
    srcLastRow = Sheets("Type_1").Range("D" & Sheets("Type_1").Rows.Count).End(xlUp).Row

    ' copy from the course sheet to the currentSheet in the range specified
    Sheets("Type_1").Range("D8:" & "D" & srcLastRow).Copy Destination:=currentsheet.Range("A" & lastrow)

    ' or maybe you want:
    ' Sheets("Type_1").Range("D8:" & "D" & srcLastRow).Copy Destination:=currentsheet.Cells(lastrow, LastCol)

End Sub