excel macro copies last row of data

eng819

New Member
Joined
Sep 26, 2011
Messages
17
I need an excel macro that will copy the last row of data from one worksheet within a workbook to another worksheet in the same work book. The last row number of data will alwasy vary :confused:
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try like this

Code:
Sub cpy()
Dim LR As Long
With Sheets("Sheet1")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    .Rows(LR).Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
End With
End Sub
 
Upvote 0
That worked!! But, I forgot to mention that it's a "copy special" and I need to copy "values" :(
 
Upvote 0
That worked!! But, I forgot to mention that it's a "copy special" and I need to copy "values" :(
Then this:
Code:
Sub CopyLastRow()
Dim lRw As Long
With ActiveSheet
    lRw = Cells.Find("*", .Range("A" & Rows.Count), , , xlByRows, xlPrevious).Row
    .Rows(lRw).Copy
End With
With Sheets("Sheet2")
    lRw = Cells.Find("*", .Range("A" & Rows.Count), , , xlByRows, xlPrevious).Row
    .Rows(lRw + 1).PasteSpecial Paste:=xlPasteValues
End With
End Sub
 
Upvote 0
used this code first and it copied last row of data to top row of another worksheet, but I need it to "paste special" and only past values.

Sub copy()
Dim LR As Long
With Sheets("1st Shift DTR MF")
LR = .Range("A" & Rows.Count).End(xlUp).Row
.Rows(LR).copy Destination:=Sheets("All DTR Mean Time MF").Range("A" & Rows.Count).End(xlUp).Offset(1)
End With
End Sub

used this code and it "paste special" the values correctly into the correct worksheet, but copied into the next row down from original row, in original worksheet. Original data in ramained selected also.

Sub CopyLastRow()
Dim lRw As Long
With ActiveSheet
lRw = Cells.Find("*", .Range("A" & Rows.Count), , , xlByRows, xlPrevious).Row
.Rows(lRw).copy
End With
With Sheets("All DTR Mean Time MF")
lRw = Cells.Find("*", .Range("A" & Rows.Count), , , xlByRows, xlPrevious).Row
.Rows(lRw + 1).PasteSpecial Paste:=xlPasteValues
End With
End Sub

I've tried some combinations of the two but still get errors.
 
Upvote 0
You are missing a dot

Rich (BB code):
Sub CopyLastRow()
Dim lRw As Long
With ActiveSheet
lRw = Cells.Find("*", .Range("A" & Rows.Count), , , xlByRows, xlPrevious).Row
.Rows(lRw).Copy
End With
With Sheets("All DTR Mean Time MF")
lRw = .Cells.Find("*", .Range("A" & Rows.Count), , , xlByRows, xlPrevious).Row
.Rows(lRw + 1).PasteSpecial Paste:=xlPasteValues
End With
End Sub
 
Upvote 0
sorry this is so confusing.

VoG - first post you sent copied last row of data from one worksheet to the top row of another worksheet. That part was correct. But I needed it to "paste special" the values instead of copy.
 
Upvote 0
Your first code was correct except for the "paste Special" values

Sub copy()
Dim LR As Long
With Sheets("1st Shift DTR MF")
LR = .Range("A" & Rows.Count).End(xlUp).Row
.Rows(LR).copy Destination:=Sheets("All DTR Mean Time MF").Range("A" & Rows.Count).End(xlUp).Offset(1)
End With


I tried using this substituting " PasteSpecial Paste:=xlPasteValues " for "copy" in the 5th row of the code and received an error

Sub copy()
Dim LR As Long
With Sheets("1st Shift DTR MF")
LR = .Range("A" & Rows.Count).End(xlUp).Row
.Rows(LR).PasteSpecial Paste:=xlPasteValues Destination:=Sheets("All DTR Mean Time MF").Range("A" & Rows.Count).End(xlUp).Offset(1)
End With
 
Upvote 0
try

Code:
Sub cpy()
Dim LR As Long
With Sheets("Sheet1")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    .Rows(LR).Copy
    Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
End With
End Sub
 
Upvote 0
If you're pasting special (Values) then why copy at all? You can simply make the values equal. (ie. - using Vog's code...)
Code:
Sub cpy()
Dim LR As Long
With Sheets("Sheet1")
    LR = .Range("A" & Rows.Count).End(xlUp).Row
    Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp)(2).EntireRow.Value = _
    .Rows(LR).EntireRow.Value
End With
End Sub

Hope it helps.
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,877
Members
452,949
Latest member
Dupuhini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top