Macro to extend range of cells

JohnTravolski

New Member
Joined
Nov 25, 2015
Messages
46
Office Version
  1. 2019
Platform
  1. Windows
I have a spreadsheet that I add rows to regularly. A particular range of these rows I copy by highlighting that range of the row above it and then dragging the plus button down in the bottom right hand corner. It's not as simple as copying the cells' values; it has to change them the exact same way it would if you do this dragging action.

I would like to automate this with a macro, but I'm not sure how to given that it always needs to find the very bottom row (but the columns never change). It should be the equivalent of doing this:


How would I write a macro to do this?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Go to Developer/Record Macro, do exactly what you did in the video, stop recording, open the recorded macro. You'll see something like this:

VBA Code:
    Range("F30:I30").Select
    Selection.AutoFill Destination:=Range("F30:I31"), Type:=xlFillDefault
    Range("F30:I31").Select
Edit it to suit your need.
 
Upvote 0
Go to Developer/Record Macro, do exactly what you did in the video, stop recording, open the recorded macro. You'll see something like this:

VBA Code:
    Range("F30:I30").Select
    Selection.AutoFill Destination:=Range("F30:I31"), Type:=xlFillDefault
    Range("F30:I31").Select
Edit it to suit your need.

I don't think this will work since it specifies the rows in the code. It has to automatically find the last row in the sheet and extend that row specifically specifically.

Which columns as you don't show it in your video?

Let's just say columns B through D.
 
Upvote 0
There are several ways to find the last non-blank row. Try this:

VBA Code:
Sub test()

Dim lrow As Double

with ActiveSheet
lrow = .Range("B1048576").End(xlUp).Row
ActiveSheet.Range("B" & lrow & ":D" & lrow).Copy Destination:=ActiveSheet.Range("B" & lrow + 1 & ":D" & lrow + 1)
End With
End Sub
 
Upvote 0
My take
VBA Code:
Sub JohnTrav()
    Dim LstRw As Long
    LstRw = Columns("B").Find("*", , xlValues, , xlByRows, xlPrevious).Row
    Range(Cells(LstRw, "B"), Cells(LstRw, "D")).Resize(2).FillDown
    Application.Goto Cells(LstRw + 1, "E")
End Sub
or
VBA Code:
Sub JohnTrav2()
    Dim LstRw As Long
    LstRw = Columns("B").Find("*", , xlValues, , xlByRows, xlPrevious).Row
    Cells(LstRw, "B").Resize(2, 3).FillDown
    Application.Goto Cells(LstRw + 1, "E")
End Sub
Although I would specify the sheet as well if you aren't using the Active sheet (which is why I have used Goto rather than Select, just in case you do need to reference the sheet later).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,115
Members
453,021
Latest member
Justyna P

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