ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   copy and paste (https://www.excelbanter.com/excel-programming/400992-copy-paste.html)

Eric

copy and paste
 
Hello all,

I am using the following macro and it works great if data is in every cell.
If there is not data in a cell then nothing in that row is copied over to
the other sheet. How can I make it work with out data in some cells?

Sub Last_four()

Sheets("Last Four").Select

Dim lastrow As Long
Dim myrow As Long
Dim mycol As Long

lastrow = Sheets("test database").Cells(Rows.count, "A").End(xlUp).Row
myrow = 1
mycol = 1
For i = 12 To 9 Step -1
Do Until Sheets("test database").Cells(lastrow, mycol) = ""
Sheets("last four").Cells(i, mycol) = Sheets("test database").Cells(lastrow,
mycol)
mycol = mycol + 1

Loop
lastrow = lastrow - 1
mycol = 1

Next

End Sub

I would appreciate all your help thanks

Eric

JW[_2_]

copy and paste
 
I could certainly be over-simplifying this, but it looks/sounds to me
like you want to copy the entire row of the last 4 rows containing
data in the Test Database sheet and place them in rows 9:12 of the
Last Four sheet, correct? If so, this will do the trick:
Sub Last_four()
Dim lastrow As Long
lastrow = Sheets("test database") _
.Cells(Rows.Count, "A").End(xlUp).Row
Sheets("test database").Rows(lastrow - 3 & _
":" & lastrow).Copy _
Sheets("last four").Cells(9, 1)
End Sub

Eric wrote:
Hello all,

I am using the following macro and it works great if data is in every cell.
If there is not data in a cell then nothing in that row is copied over to
the other sheet. How can I make it work with out data in some cells?

Sub Last_four()

Sheets("Last Four").Select

Dim lastrow As Long
Dim myrow As Long
Dim mycol As Long

lastrow = Sheets("test database").Cells(Rows.count, "A").End(xlUp).Row
myrow = 1
mycol = 1
For i = 12 To 9 Step -1
Do Until Sheets("test database").Cells(lastrow, mycol) = ""
Sheets("last four").Cells(i, mycol) = Sheets("test database").Cells(lastrow,
mycol)
mycol = mycol + 1

Loop
lastrow = lastrow - 1
mycol = 1

Next

End Sub

I would appreciate all your help thanks

Eric




All times are GMT +1. The time now is 05:34 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com