Reputation: 3
I'm using the below VBA code which is copying a range from Sheet1 and paste it in the same sheet. However i need to paste the data in the next available row of sheet2.
Private Sub CommandButton1_Click()
Sheets("Sheet1").Range("A1:A5").Copy
Dim lastrow As Long
lastrow = Range("A65536").End(xlUp).Row
Sheets("Sheet2").Activate
Cells(lastrow + 1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End Sub
Please help me out..
Upvotes: 0
Views: 4605
Reputation: 1149
First activate the sheet2 and then find last row
lastrow = Range("A65536").End(xlUp).Row
Upvotes: 0
Reputation: 942
Try this:
Private Sub CommandButton1_Click()
Dim lastrow As Long
Dim rng1 As Range
Dim rng2 As Range
lastrow = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row
Set rng1 = Sheets("Sheet1").Range("A1:A5")
Set rng2 = Sheets("Sheet2").Range("A" & lastrow + 1)
rng1.Copy
rng2.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End Sub
Upvotes: 1
Reputation: 955
Your code is good but one line you need to change it, that is place Sheets("Sheet2").Activate
line before lastrow = Range("A65536").End(xlUp).Row
Private Sub CommandButton1_Click()
Sheets("Sheet1").Range("A1:A5").Copy
Sheets("Sheet2").Activate
Dim lastrow As Long
lastrow = Range("A65536").End(xlUp).Row
Cells(lastrow + 1, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End Sub
Upvotes: 0