PDA

View Full Version : Solved: Copy/Paste strange thing happening



Johnpants
11-22-2005, 03:49 AM
Hi, I have written this code to copy a cell from one sheet, then paste it into the next free row on another, but for some reason it pastes it to row 58, and if I do it again, rather than pasting to the next row it just pastes over the top of the last one..

Could anyone tell me where i'm going wrong please?

Thanks.


Private Sub CommandButton1_Click()
LastRow = Range("A65536").End(xlUp).Row

Sheets("Order").Range("A22").copy
Sheets("Data").Select
Cells(LastRow + 1, 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False

Sheets("Order").Range("A24").copy
Sheets("Data").Select
Cells(LastRow + 1, 2).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False

Sheets("Order").Range("A26").copy
Sheets("Data").Select
Cells(LastRow + 1, 3).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False

Unload Me
End Sub

Killian
11-22-2005, 05:39 AM
Which LastRow are you finding? Without specifying the sheet, it will always be the active one.
Since Data is the target sheet, you should change that to LastRow = Sheets("Data").Range("A65536").End(xlUp).Row
Also, not that this may be an issue, but using End.(xlUp) will return 1 is the sheet is empty. This might be worth testing forFunction TargetRow(ws As Worksheet) As Long
'function to return the row index of the first empty row
Dim lngLastRow As Long
lngLastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
If IsEmpty(ws.Cells(lngLastRow, 1)) Then
TargetRow = 1
Else
TargetRow = lngLastRow + 1
End If
End Function

Johnpants
11-22-2005, 06:52 AM
Thank you, works perfectly! :)