Rng1.Copy Destination:=Worksheets("RefindData").Range(Destination)
Rng1
是要复制的数据范围,Destination
当前是单元格引用(E.g B2
)
此代码将被多次调用。我如何改变这一点,以便目的地是同一列(例如,列B),但该行是下一个空单元格?
例如,在第一次调用时,B2向前是值被复制到的位置,然后在下一次调用时,第一次调用之后的第二个空单元格应该开始输出其值。然后是第三个调用开始的下一个空单元格,依此类推。
我可以将Destination
变量更改为仅列出列字母,如下所示:
Rng1.Copy Destination:=Worksheets("RefindData").Range(Destination & ???)
沿着正确的路线?
答案 0 :(得分:3)
Sub CopyPasteCells()
Dim Rng1 As Range, Rng2 As Range, ws As Worksheet
Set ws = Worksheets("RefindData")
Set Rng1 = ws.Range("C2:C10") 'Copy range as you like
Set Rng2 = ws.Range("B" & ws.Rows.Count).End(xlUp).Offset(1, 0) 'Paste range starting from B2 and then first empty cell
Rng1.Copy Destination:=Rng2 'Copy/Paste
End Sub
答案 1 :(得分:2)
您也可以尝试下面的代码。
假设:
代码
Sub CutCopyPaste()
Dim lngCol As Long
Dim rngCopy As Range
Set rngCopy = Range("A1") 'The cell which ic copied
lngCol = Selection.Column 'active column where the results will be pasted
On Error Resume Next
rngCopy.Copy Cells(Cells(1, lngCol).End(xlDown).Row + 1, lngCol)
If Err.Number = 1004 Then
MsgBox "Be sure that active cell is in the column, where the results should be pasted!" & vbNewLine & vbNewLine & "Try again"
Err.Clear
End If
End Sub
答案 2 :(得分:1)
你的意思是这样吗?
Sub Sample()
Dim rng1 As Range
Dim wsO As Worksheet
Set wsO = Worksheets("RefindData")
Set rng1 = Range("A1:A10")
rng1.Copy Destination:=wsO.Range("B" & _
wsO.Range("B" & wsO.Rows.Count).End(xlUp).Row + 1)
End Sub
每次运行宏时,它都会粘贴到最后一行之后的下一个可用行中。