![](/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.