![](/img/trans.png)
[英]Trying to copy a range from one sheet and paste it to the next empty cell in a column on another sheet
[英]Copy & Paste range as values into next empty column on another sheet
我正在尝试将一个范围(根据输入而变化)作为值复制到另一张表中。
下面的代码似乎是复制到最后一个条目的顶部,而不是复制到下一个空列中。
Sub CommandButton3_Click()
Dim source As Worksheet
Dim destination As Worksheet
Dim emptyColumn As Long
Set source = Sheets("Month Template")
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("m26:m35").Copy destination.Cells(1, emptyColumn)
End Sub
试试下面
Sub CommandButton3_Click()
Dim source As Worksheet
Dim destination As Worksheet
Dim emptyColumn As Long
Set source = Sheets("Month Template")
Set destination = Sheets("Sheet1")
'find empty Column (actually cell in Row 1)'
emptyColumn = destination.Cells(1, 1).End(xlToRight).Column
If emptyColumn > 1 Then
emptyColumn = emptyColumn + 1
End If
source.Range("m26:m35").Copy destination.Cells(1, emptyColumn)
End Sub
这是一个老生常谈的问题。 在阅读 Op 的问题时,他们似乎想确保使用第一列,然后移至第二列。
If emptyColumn > 1 Then
第一次使用复制粘贴代码时, emptycolumn
永远不会大于 1。
一个可能的解决方案是在 emptycolumn=2 时第一次检查 A1 =“”。
Private Sub CommandButton3_Click()
Dim source As Worksheet
Dim destination As Worksheet
Dim emptyColumn As Long
Set source = Sheets("Month Template")
Set destination = Sheets("Sheet1")
'find empty Column (actually cell in Row 1)'
emptyColumn = destination.Cells(1, destination.Columns.Count).End(xlToLeft).Column + 1
If destination.Cells(1, 1) = "" And emptyColumn = 2 Then emptyColumn = 1
source.Range("m26:m35").Copy destination.Cells(1, emptyColumn)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.