[英]Copy & Paste range as values into next empty column on another sheet
I am trying to copy a range (which varies based on inputs) as values into another sheet.我正在尝试将一个范围(根据输入而变化)作为值复制到另一张表中。
The code below seems to be copying on top of the last entry rather than into the next empty column.下面的代码似乎是复制到最后一个条目的顶部,而不是复制到下一个空列中。
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
try with below试试下面
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
This is an old question that popped up.这是一个老生常谈的问题。 When reading the Op's question, it looks like they want to make sure the 1st column is used and then move over to column 2.
在阅读 Op 的问题时,他们似乎想确保使用第一列,然后移至第二列。
If emptyColumn > 1 Then
When 1st using the copy&paste code emptycolumn
will never be greater than 1.第一次使用复制粘贴代码时,
emptycolumn
永远不会大于 1。
A possible solution would be to 1st check if A1 ="" when emptycolumn=2.一个可能的解决方案是在 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.