简体   繁体   English

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

[英]copy paste range in first empty column in excel vba

I want to copy the range of cells (C1:Z1000) of worksheet3 and paste them to the first empty column of worksheet1 (in row 1). 我想复制工作表3的单元格范围(C1:Z1000)并将它们粘贴到工作表1的第一个空列(第1行)。 The code below blocks at the last line: source.Range("C1:Z1000").Copy destination.Cells(1, emptyColumn) 下面的代码在最后一行阻塞:source.Range(“C1:Z1000”)。复制destination.Cells(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(xlUp).Column
If emptyColumn > 1 Then
emptyColumn = emptyColumn + 1
End If

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

End Sub

I think your issue was the way you were obtaining the emptyColumn value, as others suggested. 我认为您的问题是您获取emptyColumn值的方式,正如其他人所建议的那样。 This works for me: 这对我有用:

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

The way you currently have it will pull the very last column in the worksheet, which seems to thrown an error when pasting to it. 您当前拥有它的方式将拉出工作表中的最后一列,这似乎在粘贴时会抛出错误。 The above approach will pull the very first empty column. 上面的方法将拉出第一个空列。 That is, if column C is empty, the value of emptyColumn will be 3 也就是说,如果列C为空,则emptyColumn的值将为3

Have you tried stepping through your code? 您是否尝试过单步执行代码?

If you do, you'll notice that the following line will always set the emptyColumns variable to the far right column, regardless of which columns are used: 如果这样做,您会注意到以下行始终将emptyColumns变量设置为最右侧列,无论使用哪些列:

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

By adding 1 to it and pasting you try to paste to a column that does not exist. 通过向其添加1并粘贴您尝试粘贴到不存在的列。 That will give you an error every time. 每次都会给你一个错误。

Instead, try the following to find the last used column. 相反,请尝试以下操作以查找上次使用的列。 It searches from the far right column in the first row and goes left (like typing CTRL + LEFT ), in order to find the last used column: 它从第一行的最右列开始搜索并向左移动(如键入CTRL + LEFT ),以便查找上次使用的列:

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

Then you can add 1 to it and paste. 然后你可以添加1并粘贴。

try this: 尝试这个:

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

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

named arguments are followed by a colon equals := 命名参数后跟冒号等于:=

your code for End should be: End(xlToRight) 你的End代码应该是: End(xlToRight)

another alternative would be: 另一种选择是:

source.Range("C1:Z1000").Copy 
with destination
    .cells(1,emptycolumn).select
    .paste
end with

I hope that helps 我希望有所帮助

Philip 菲利普

I found this post, modified it to fit my needs. 我发现这篇文章,修改它以满足我的需要。 Will paste transpose 将粘贴转置

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

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

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