Macro question: how to copy the last row from a range with criteria by means of macro

Paulus1983

New Member
Joined
Jan 7, 2008
Messages
12
Hello! I would very much appreciate it if someone could help me with a specific macro question.
Question: what will a macro look like that allows me to copy the final row from a specific range (eg. B5:M27) to the next row The first row (row 4) cannot be copied, this is a permanent / descriptive row. So I'm looking for a macro that searches for the final row in a pre-set range and copies this row to the next, when the macro is activated.

- In the example above the final row can be ranging from 5 to 27.

I really would appreciate your help!

Thanks in advance
 
This worked great but I would like to change the criteria. Instead of determining the row to copy based on the final row that contains data in column B, I would like to determine the row to copy based on the cell selected in column A.
eg. if the relevant range is B5:M27 and A8 is selected, the macro should copy B8:M8 and past it to the next.

Thank you very much for all help!


This additional question is also posted in a seperate thread:
http://www.mrexcel.com/forum/showthread.php?t=539674

----------------------------------------------------------------------
 
Upvote 0

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Not testes

Code:
Sub CopyRow()
Dim Last as Long
Last = ActiveCell.Row
Range(Cells(Last + 1, "B"), (Cells(Last + 1, "M"))).Value = Range(Cells(Last, "B"), (Cells(Last, "M"))).Value
End Sub
</pre>
 
Upvote 0
Hi Jen,

Thank you again for your help. The code works very well. I'm starting to get a little familiar with VBE and combined your code with another code I received:

Option Explicit

Sub CopyRow()

Dim r As Long
Dim NextRow As Long

If Not Intersect(Range("A5:A27"), ActiveCell) Is Nothing Then

r = ActiveCell.Row

NextRow = Cells(Rows.Count, "B").End(xlUp).Row + 1

Range(Cells(r, "B"), Cells(r, "M")).Copy Destination:=Cells(NextRow, "B")

Else

MsgBox "Active cell is not located within A5:A27...", vbExclamation

End If

End Sub



And came up with the following:

Option Explicit

Sub CopyRow()

Dim last As Long

If Not Intersect(Range("A5:A27"), ActiveCell) Is Nothing Then

Last = ActiveCell.Row

Range(Cells(Last + 1, "B"), (Cells(Last + 1, "M"))).Value = Range(Cells(Last, "B"), (Cells(Last, "M"))).Value

Else

MsgBox "Active cell is not located within A5:A27...", vbExclamation

End If

End Sub

This code follows the criteria that the active cell should be placed in column A but doesn't make a count in column B to find out what the final cell is.

Still don't know exactly what it says but I pretty much understand what it does.

Thanks very much!
 
Upvote 0
Try this
Rich (BB code):
Option Explicit

Sub CopyRow()

Dim Last As Long
Dim Next as long

If Not Intersect(Range("A5:A27"), ActiveCell) Is Nothing Then

Last = ActiveCell.Row
Next = Cells(Rows.Count, "B").End(xlUp).Row + 1

Range(Cells(Next, "B"), (Cells(Next, "M"))).Value = Range(Cells(Last, "B"), (Cells(Last, "M"))).Value

Else

MsgBox "Active cell is not located within A5:A27...", vbExclamation

End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,753
Members
452,940
Latest member
rootytrip

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