复制excel vba中第一个空列中的粘贴范围

复制excel vba中第一个空列中的粘贴范围,vba,excel,copy-paste,Vba,Excel,Copy Paste,我想复制工作表3的单元格范围(C1:Z1000),并将它们粘贴到工作表1的第一个空列(第1行)。下面的代码在最后一行阻塞:source.Range(“C1:Z1000”).Copy destination.Cells(1,emptyColumn) 试试这个: emptyColumn = destination.Cells(1, destination.Columns.Count).End(xltoright).Column source.Range("C1:Z1000").Copy Desti

我想复制工作表3的单元格范围(C1:Z1000),并将它们粘贴到工作表1的第一个空列(第1行)。下面的代码在最后一行阻塞:source.Range(“C1:Z1000”).Copy destination.Cells(1,emptyColumn)

试试这个:

emptyColumn = destination.Cells(1, destination.Columns.Count).End(xltoright).Column

source.Range("C1:Z1000").Copy Destination:= Cells(1, emptyColumn)
命名参数后跟冒号equals:=

End的代码应该是:End(xlToRight)

另一种选择是:

source.Range("C1:Z1000").Copy 
with destination
    .cells(1,emptycolumn).select
    .paste
end with
我希望这有帮助


Philip

您是否尝试过单步执行代码

如果这样做,您会注意到,无论使用哪一列,下一行总是将
emptyColumns
变量设置为最右列:

emptyColumn = destination.Cells(1, destination.Columns.Count).End(xlUp).Column
通过向其中添加1并粘贴,可以尝试粘贴到不存在的列。每次都会给你一个错误

相反,请尝试以下操作以查找最后使用的列。它从第一行最右边的列开始搜索,然后向左(如键入CTRL+left),以查找最后使用的列:

emptyColumn = destination.Cells(1, destination.Columns.Count).End(xlToLeft).Column

然后您可以向其中添加1并粘贴。

我认为您的问题在于如何获得
emptyColumn
值,正如其他人所建议的那样。这对我很有用:

Sub CopyRange()

Dim source As Worksheet
Dim destination As Worksheet
Dim emptyColumn As Long

Set source = Sheets("Sheet3")
Set destination = Sheets("Sheet1")

'find empty Column (actually cell in Row 1)'
emptyColumn = destination.Cells(1, destination.Columns.Count).End(xlToLeft).Column
If emptyColumn > 1 Then
emptyColumn = emptyColumn + 1
End If

source.Range("C1:Z1000").Copy destination.Cells(1, emptyColumn)

End Sub

您当前使用的方式将拖拽工作表中的最后一列,粘贴到该列时似乎会抛出错误。上述方法将提取第一个空列。也就是说,如果C列是空的,
emptyColumn
的值将是3

我找到了这篇文章,修改它以满足我的需要。将粘贴转置

Sub Transpose_PasteModified()

Dim source As Worksheet
Dim destination As Worksheet
Dim emptyColumn As Long

Set source = Sheets("Sheet1")
Set destination = Sheets("Sheet2")
'Data Source
source.Range("A3:C3").Copy
'Gets Empty Column
emptyColumn = destination.Cells(3, destination.Columns.Count).End(xlToLeft).Column

'In order to avoid issues of first row returning 1
'in this case #3 is the row so we're checking A3
'If row changes then change A3 to correct row

If IsEmpty(destination.Range("A3")) Then
destination.Cells(3, 1).PasteSpecial Transpose:=True
Else
emptyColumn = emptyColumn + 1
destination.Cells(3, emptyColumn).PasteSpecial Transpose:=True
End If

End Sub

好的,你从哪里得到错误?也许试试下面我给你的第二个选择。另外,Sourece是一个像工作表或工作簿这样的对象,还是一个范围?我认为最后一列是.End(xlToRight)。column不,这是设计的。我从最右边开始(<代码>目的地。列。计数< /代码>),然后向左,这样我就不会被困在中间的空列中,右边使用更多的列。你的代码不合逻辑。检查sheet3的最后一列是否为空,如果不是,则取下一列。这当然永远不会存在。
Sub Transpose_PasteModified()

Dim source As Worksheet
Dim destination As Worksheet
Dim emptyColumn As Long

Set source = Sheets("Sheet1")
Set destination = Sheets("Sheet2")
'Data Source
source.Range("A3:C3").Copy
'Gets Empty Column
emptyColumn = destination.Cells(3, destination.Columns.Count).End(xlToLeft).Column

'In order to avoid issues of first row returning 1
'in this case #3 is the row so we're checking A3
'If row changes then change A3 to correct row

If IsEmpty(destination.Range("A3")) Then
destination.Cells(3, 1).PasteSpecial Transpose:=True
Else
emptyColumn = emptyColumn + 1
destination.Cells(3, emptyColumn).PasteSpecial Transpose:=True
End If

End Sub