Last Row Autofill - Slight Twist

aeddipa

Active Member
Joined
Jun 1, 2009
Messages
337
Hello,

I have code where I am looking to autofill a selected range to the last row of data. I generally know how to build the code correctly, but I have a problem with the beginning of the range.

Code:
Range("E2:F2").Select
Selection.PasteSpecial xlPasteValues
LR = Range("A" & Rows.Count).End(xlUp).Row
Selection.AutoFill Destination:=Range("E:F" & LR)

The first line of code was written so people can follow, but it will change as it is a loop macro and I have a relative reference. The issue in the code is obviously the last line. Generally it would be the following:

Code:
Selection.AutoFill Destination:=Range("E2:F" & LR)

where an actual row is given for the first set of range. However, since this row number can always change, how can I build it to accomodate my needs? The columns will always be E & F.

Don't worry about Column A as LR. I need it to be that way.

Any help would be greatly appreciated!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Are you saying that the first row won't always be E2? How is it variable? From nothing at all in column E or just varies between row 2 to row 10? There's lots of variables.

I'm throwing this out as just a question to see if it will work for you.... Can you use the last row - 1 with an end up to determine the first in column E? It was just a thought, totally untested to see if you can.

Code:
Range("E2:F2").Select
Selection.PasteSpecial xlPasteValues
lr = Range("A" & Rows.Count).End(xlUp).Row
fr = Range("E" & lr - 1).End(xlUp).Row
Selection.AutoFill Destination:=Range("E" & fr & ":F" & lr)
 
Upvote 0
That worked incredibly well. Yes, column E does change each time and that is why there is the unknown row number. You illustrated my request perfectly! My only issue is that the data that is in Column E is currently "General" formatting but has numbers. Example: 5712. Due to it not being a number, when autofill it goes 5713, 5714, etc... I need the number to remain the same throughout each autofill row. I tried formatting to number, but the only way to correct it is by then clicking F2 and enter to refresh the cell. Is there a way to refresh the cell after formatting to Number?

Code:
ActiveCell.NumberFormat = "0"

Are you saying that the first row won't always be E2? How is it variable? From nothing at all in column E or just varies between row 2 to row 10? There's lots of variables.

I'm throwing this out as just a question to see if it will work for you.... Can you use the last row - 1 with an end up to determine the first in column E? It was just a thought, totally untested to see if you can.

Code:
Range("E2:F2").Select
Selection.PasteSpecial xlPasteValues
lr = Range("A" & Rows.Count).End(xlUp).Row
fr = Range("E" & lr - 1).End(xlUp).Row
Selection.AutoFill Destination:=Range("E" & fr & ":F" & lr)
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,676
Members
448,977
Latest member
moonlight6

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