relative reference to replace fixed reference in code

j3andc

Board Regular
Joined
Mar 4, 2002
Messages
172
I have a macro with the following line of code

Selection.AutoFill Destination:=Range("E2:E390")

The problem is that the number of rows will not aways be the same. How do I change the code so that it finds the bottow rather than me telling it where the bottom is.

Thanks in advance for any help.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Yes, all of the columns contain the same amount of data.

in the code, column E is created new and then a formula is populated into the new column for every row that contains data.
 
Upvote 0
You could use something like this:

Code:
Range(Selection, Selection.End(xlDown)).Select

in your code to just move to the end. You could also make the formula leave the cell blank when nothing should be displayed there and then copy and paste values for the entire column after that. Hope this helps.
 
Upvote 0
Code:
Sub Foo()
Set SourceRange = Worksheets("Sheet1").Range("E1")
LR = Range("A" & Rows.Count).End(xlUp).Row
Set fillRange = Worksheets("Sheet1").Range("E1:E" & LR)
SourceRange.AutoFill Destination:=fillRange
End Sub
 
Upvote 0
With this command (Range(Selection, Selection.End(xlDown)).Select), the selection range goes well beyond the last row of data.
 
Last edited:
Upvote 0
Select the top cell in the column before that line. It's directly out of the macro recorder, so it can be cleaned up some as far as the VB goes, but it should do what you need.
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,826
Members
449,190
Latest member
rscraig11

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