Copy and Paste LAST ROW of data

  • Thread starter Sam via OfficeKB.com
  • Start date
S

Sam via OfficeKB.com

Hi All,

I would like to:

1) Find the LAST row of consecutive data (starts at column "A" - numeric
values in col "A", start row number varies)

2) Selct the FIRST to LAST cell of continuous data on that LAST row

3) Copy the FIRST to LAST cell of continuous data on that LAST row

4) Paste the copied data in the same cell position on the row immediately
below

If possible, can a solution be provided to do the above process on multiple
worksheets within the same workbook?

Thanks,
Sam
 
G

Guest

If A13 and B13 have values, C13 is empty, and D13 has a value,
then
do you want A13 thru B13 or A13 thru D13?
 
S

Sam via OfficeKB.com

Hi Gary''s Student,

Thank you for reply.

I would like A13 thru B13

Cheers,
Sam
 
M

Mike Fogleman

Is there other data surrounding this contiguous data that we need to avoid?
If not, then this simple code will do it:

Sub test()
Dim LRow As Long

LRow = Cells(Rows.Count, 1).End(xlUp).Row
Rows(LRow).Copy Rows(LRow + 1)
End Sub

Mike F
 
S

Sam via OfficeKB.com

Hi Mike,

Thanks for reply and assistance.

Does your solution take into account Gary's Student Post:
If A13 and B13 have values, C13 is empty, and D13 has a value,
then
do you want A13 thru B13 or A13 thru D13?

I would like A13 thru B13
Mike Fogleman wrote:
Is there other data surrounding this contiguous data that we need to avoid?
If not, then this simple code will do it:

Sub test()
Dim LRow As Long

LRow = Cells(Rows.Count, 1).End(xlUp).Row
Rows(LRow).Copy Rows(LRow + 1)
End Sub

Mike, will this code work with my request for A13 thru B13 based on above,
selecting only contiguous data filled cells on that row?

Please advise.

Cheers,
Sam
 
G

Guest

Sub CopyLast()
Dim r1 as Range, r2 as Range
set r1 = cells(rows.count,1).End(xlup)
if isempty(r1(1,2)) then
set r2 = r1
else
set r2 = r1.end(xltoRight)
end if
Range(r1,r2).Copy r1(2)
End Sub
 
M

Mike Fogleman

This one does exclude any data on the last row that is to the right of a
blank cell. However, if there is data further down in column A after some
blank cells then this will find it and copy that row. More code would be
needed to avoid data that is below the contiguous data in column A.

Sub test()
Dim LRow As Long
Dim MyRng As Range

LRow = Cells(Rows.Count, 1).End(xlUp).Row
Set MyRng = Range("A" & LRow)
Range(MyRng, MyRng.End(xlToRight)).Copy Range("A" & LRow + 1)
End Sub

Mike F
 
M

Mike Fogleman

Hi Tom, neither of our code addresses the situation if there is
non-contiguous data further down in column A. Awaiting the op's answer on
that.

Mike F
 
S

Sam via OfficeKB.com

Hi Tom,

Thank you very much for reply and assistance.

Your code does the job Brilliantly!

However, could you adapt it to perform this routine on multiple worksheets
within the same workbook.

Further assistance very much appreciated.

Cheers
Sam
Tom Ogilvy wrote:
Sub CopyLast()
Dim r1 as Range, r2 as Range
set r1 = cells(rows.count,1).End(xlup)
if isempty(r1(1,2)) then
set r2 = r1
else
set r2 = r1.end(xltoRight)
end if
Range(r1,r2).Copy r1(2)
End Sub
 
S

Sam via OfficeKB.com

Hi Mike,

Thank you very much indeed for your time and assistance.

Your code works Great!
However, if there is data further down in column A after some
blank cells then this will find it and copy that row. More code would be
needed to avoid data that is below the contiguous data in column A.

Would it be possible for you to extend your Sub test() routine and provide
the additional code that would take the above scenario into account.

Much appreciated.

Cheers,
Sam

Mike said:
This one does exclude any data on the last row that is to the right of a
blank cell. However, if there is data further down in column A after some
blank cells then this will find it and copy that row. More code would be
needed to avoid data that is below the contiguous data in column A.
Sub test()
Dim LRow As Long
Dim MyRng As Range
 
M

Mike Fogleman

Sam, this will work to some extent if there is data below the target range,
it is also contiguous, and there is only one section of data. I have also
modified the code to run on each sheet in the workbook, as you requested of
Tom.
If there are several areas of data below the target data, then we will need
a way to differentiate among them to find the range.

Sub test()
Dim LRow As Long
Dim MyRng As Range
Dim i As Long
Dim ws As Worksheet

For Each ws In Worksheets
ws.Activate
LRow = Cells(Rows.Count, 1).End(xlUp).Row
Set MyRng = Range("A" & LRow)
i = MyRng.CurrentRegion.Rows.Count
i = LRow - i
If IsEmpty(Range("A" & i)) Then
i = i - 1
If i = 0 Then i = 1
Set MyRng = Range("A" & i)
Do While IsEmpty(MyRng)
i = i - 1
If i = 0 Then i = 1
Set MyRng = Range("A" & i)
If MyRng.Row = 1 Then
LRow = Cells(Rows.Count, 1).End(xlUp).Row
Set MyRng = Range("A" & LRow)
Exit Do
End If
Loop
LRow = MyRng.Row
Range(MyRng, MyRng.End(xlToRight)).Copy Range("A" & LRow + 1)
End If
Next
Worksheets(1).Activate
End Sub

Mike F
 
S

Sam via OfficeKB.com

Hi Mike,

Thank you for additional help.

When I run Sub test(), I get run-time error '1004' on this line: If IsEmpty
(Range("A" & i)) Then

error message Method 'range' of object '_Global' failed.
I have also modified the code to run on each sheet in the workbook, as you requested of
Tom.

Could the above be specific sheets I list in an array, rather than all sheets?


Please advise.

Very much appreciated.

Cheers,
Sam

Mike said:
Sam, this will work to some extent if there is data below the target range,
it is also contiguous, and there is only one section of data. I have also
modified the code to run on each sheet in the workbook, as you requested of
Tom.
If there are several areas of data below the target data, then we will need
a way to differentiate among them to find the range.
Sub test()
Dim LRow As Long
Dim MyRng As Range
Dim i As Long
Dim ws As Worksheet
 
G

Guest

I am not sure what dragon Mike is trying to slay - I didn't see it in your
initial description. I guess cells(rows.count,1).offset(1,1) being not
empty, but then I am not sure why you would be copying data to that row.

anyway

Sub CopyLast()
Dim r1 as Range, r2 as Range
Dim sh as worksheet
for each sh in worksheets(Array("sheet1", "sheet3", "sheet5"))
set r1 = cells(rows.count,1).End(xlup)
if isempty(r1(1,2)) then
set r2 = r1
else
set r2 = r1.end(xltoRight)
end if
Range(r1,r2).Copy r1(2)
Next sh
End Sub
 
G

Guest

After investing more time, I guess he means if there are multiple areas in
column A.

Assuming the values are constants and not formulas, then this modification
should handle that.

Sub CopyLast()
Dim r1 as Range, r2 as Range
Dim sh as worksheet
for each sh in worksheets(Array("sheet1", "sheet3", "sheet5"))
set r1 = Columns(1).specialCells(xlConstants,xlNumbers).Areas(1)
set r1 = r1(r1.count)
if isempty(r1(1,2)) then
set r2 = r1
else
set r2 = r1.end(xltoRight)
end if
Range(r1,r2).Copy r1(2)
Next sh
End Sub

this assumes going down column A

1 or more blank cells

a contiguous range of cells with number constants

at least one blank cell (then there may be:

other data in cells including blanks. )

If it is different from that, say how different.
 
S

Sam via OfficeKB.com

Hi Tom,

Thank you very much for further input.

When I run Sub CopyLast(), it is adding two new rows of data to the first
worksheet and no new row to the second worksheet specified.

I have replied to your questions below.

Cheers
Sam

Tom said:
After investing more time, I guess he means if there are multiple areas in
column A.
Assuming the values are constants and not formulas, then this modification
should handle that.
Sub CopyLast()
Dim r1 as Range, r2 as Range
Dim sh as worksheet
for each sh in worksheets(Array("sheet1", "sheet3", "sheet5"))
set r1 = Columns(1).specialCells(xlConstants,xlNumbers).Areas(1)
set r1 = r1(r1.count)
if isempty(r1(1,2)) then
set r2 = r1
else
set r2 = r1.end(xltoRight)
end if
Range(r1,r2).Copy r1(2)
Next sh
End Sub
this assumes going down column A Yes

1 or more blank cells
Yes, but before the start of contiguous range of cells and possibly after.
a contiguous range of cells with number constants Yes

at least one blank cell (then there may be: sometimes

other data in cells including blanks. ) sometimes

If it is different from that, say how different.
If there is at least one blank cell below the contiguous range of cells, I
would like that data excluded from the copy process; the last row should be
qualified by the end of the contiguous data, anything after the contiguous
data should be excluded.
I am not sure what dragon Mike is trying to slay - I didn't see it in your
initial description. I guess cells(rows.count,1).offset(1,1) being not
[quoted text clipped - 78 lines]
 
M

Mike Fogleman

Add one more line after the For Each statement:

for each sh in worksheets(Array("sheet1", "sheet3", "sheet5"))
sh.Activate

Since the main code does not qualify which sheet the ranges, rows, columns,
etc are on, you need to Activate each sheet in its' turn so the code will
act upon the proper sheet.

Mike F
Sam via OfficeKB.com said:
Hi Tom,

Thank you very much for further input.

When I run Sub CopyLast(), it is adding two new rows of data to the first
worksheet and no new row to the second worksheet specified.

I have replied to your questions below.

Cheers
Sam

Tom said:
After investing more time, I guess he means if there are multiple areas in
column A.
Assuming the values are constants and not formulas, then this modification
should handle that.
Sub CopyLast()
Dim r1 as Range, r2 as Range
Dim sh as worksheet
for each sh in worksheets(Array("sheet1", "sheet3", "sheet5"))
set r1 = Columns(1).specialCells(xlConstants,xlNumbers).Areas(1)
set r1 = r1(r1.count)
if isempty(r1(1,2)) then
set r2 = r1
else
set r2 = r1.end(xltoRight)
end if
Range(r1,r2).Copy r1(2)
Next sh
End Sub
this assumes going down column A Yes

1 or more blank cells
Yes, but before the start of contiguous range of cells and possibly after.
a contiguous range of cells with number constants Yes

at least one blank cell (then there may be: sometimes

other data in cells including blanks. ) sometimes

If it is different from that, say how different.
If there is at least one blank cell below the contiguous range of cells, I
would like that data excluded from the copy process; the last row should
be
qualified by the end of the contiguous data, anything after the contiguous
data should be excluded.
I am not sure what dragon Mike is trying to slay - I didn't see it in
your
initial description. I guess cells(rows.count,1).offset(1,1) being not
[quoted text clipped - 78 lines]
 
S

Sam via OfficeKB.com

Hi Mike,

That has done the trick!

A huge Thank You to both you and Tom, for all your time and patience.

Most appreciated.

Cheers,
Sam

Mike said:
Add one more line after the For Each statement:
for each sh in worksheets(Array("sheet1", "sheet3", "sheet5"))
sh.Activate
 
G

Guest

Forgot to qualify Columns.

Sub CopyLast()
Dim r1 as Range, r2 as Range
Dim sh as worksheet
for each sh in worksheets(Array("sheet1", "sheet3", "sheet5"))
set r1 = sh.Columns(1).specialCells(xlConstants,xlNumbers).Areas(1)
set r1 = r1(r1.count)
if isempty(r1(1,2)) then
set r2 = r1
else
set r2 = r1.end(xltoRight)
end if
Range(r1,r2).Copy r1(2)
Next sh
End Sub

--
Regards,
Tom Ogilvy


Sam via OfficeKB.com said:
Hi Tom,

Thank you very much for further input.

When I run Sub CopyLast(), it is adding two new rows of data to the first
worksheet and no new row to the second worksheet specified.

I have replied to your questions below.

Cheers
Sam

Tom said:
After investing more time, I guess he means if there are multiple areas in
column A.
Assuming the values are constants and not formulas, then this modification
should handle that.
Sub CopyLast()
Dim r1 as Range, r2 as Range
Dim sh as worksheet
for each sh in worksheets(Array("sheet1", "sheet3", "sheet5"))
set r1 = Columns(1).specialCells(xlConstants,xlNumbers).Areas(1)
set r1 = r1(r1.count)
if isempty(r1(1,2)) then
set r2 = r1
else
set r2 = r1.end(xltoRight)
end if
Range(r1,r2).Copy r1(2)
Next sh
End Sub
this assumes going down column A Yes

1 or more blank cells
Yes, but before the start of contiguous range of cells and possibly after.
a contiguous range of cells with number constants Yes

at least one blank cell (then there may be: sometimes

other data in cells including blanks. ) sometimes

If it is different from that, say how different.
If there is at least one blank cell below the contiguous range of cells, I
would like that data excluded from the copy process; the last row should be
qualified by the end of the contiguous data, anything after the contiguous
data should be excluded.
I am not sure what dragon Mike is trying to slay - I didn't see it in your
initial description. I guess cells(rows.count,1).offset(1,1) being not
[quoted text clipped - 78 lines]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top