如何在VBA中将列从一张图纸粘贴到另一张图纸?

如何在VBA中将列从一张图纸粘贴到另一张图纸?,vba,excel,Vba,Excel,我在一张工作表中有一个行数未知的列,我想复制它并粘贴到另一张工作表上。由于行数未知,我将其定义为一个变量: 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("Typ

我在一张工作表中有一个行数未知的列,我想复制它并粘贴到另一张工作表上。由于行数未知,我将其定义为一个变量:

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
我发现此宏有错误,也许有人可以帮助我?

您可以尝试:

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

你错过了科隆。更改
范围(“D8”和“D”&LastRow)。将
复制到
范围(“D8:D”&LastRow)。复制
。顺便说一句,在为活动工作表定义的代码
lastrow
中,从工作表
中复制数据时,键入_1
。这是正确的行为吗?谢谢!我改了,但现在它什么也不复制了。LastRow变量定义可能有问题吗?我可能应该为类型_1定义它。问题是我必须从许多不同的工作表中复制列,是否必须为每个列定义lastrow?在代码
lastrow
中,为活动工作表定义,而从工作表
中复制数据时,键入\u 1
。尝试使用
LastRow=Sheets(“Type_1”).Range(“A65536”).End(xlUp)。Row
不幸的是,即使将Range更改为Range(“D8:D”&LastRow)。Copy和LastRow=Sheets(“Type_1”).Range(“A65536”).End(xlUp)。Row它不进行复制/粘贴,但也不会给出错误。在复制之前尝试添加行
MsgBox LastRow
。你会得到什么结果?顺便说一句,也许您应该使用列
D
确定lastrow,如下所示:
lastrow=Sheets(“Type_1”).Range(“D”和Rows.Count”).End(xlUp)。Row
?谢谢您,Philip,它已经解决了,但我认为您的解决方案也很好!