使用命令按钮,我试图将范围(根据输入而变化)作为值复制到另一个工作表中。 我有下面提到的代码,但它似乎是复制在最后一个条目的顶部,而不是粘贴到下一个空列。我对VBA很新,所以一步一步的帮助将不胜感激。谢谢
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
答案 0 :(得分:0)
尝试使用以下
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
答案 1 :(得分:0)
这是一个古老的问题。 阅读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