문제

Scenario:

I have a consistent formula starting in Range("V4").

This range has a Defined Name: Stock4 (This is so the formula/cell can be relatively referenced if the user adds or removes surrounding columns).

The spreadsheet is constantly updated with fresh data (Pasted in Range("A4:U4")).

The amount of pasted rows varies with each batch of data.

I need to extend the formula within Stock4 down to the last row...

What I'm doing:

So, I'm using a combination of...

Dim Lastrow As Integer
Lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row

And

.AutoFill Destination:=Range(*insert range here*), Type:=xlFillDefault

And my cell's Defined Name: Stock4.

Here is what I get:

Range("Stock4").AutoFill Destination:=Range("Stock4:$V" & Lastrow), Type:=xlFillDefault

This works very well, however...

$V

...is not relative.

My attempted solutions:

I have tried using Split(ActiveCell.Address(1, 0), "$")(0) & Lastrow

This returns the ActiveCell Column Letter: V and Lastrow Row Number: 4831.

However...

I can't simply substitute this code into my existing code in place of $V" & Lastrow (see below)

Range("Stock4").AutoFill Destination:=Range("Stock4:Split(ActiveCell.Address(1, 0), "$")(0) & Lastrow"), Type:=xlFillDefault

Syntax errors aside, this simply won't work.

Question:

How can I extend the formula located in Stock4 down to the last row of Stock4's column (relative reference) using VBA code.

(This code is then going to be adapted and repeated for multiple relative columns across multiple spreadsheets)

To sum up:

Extend Formulas Down To Lastrow Using Defined Name Ranges And Relative Columns. (I have spent the best part of 2 working days going grey with this problem - Is it possible? It must be possible!)

도움이 되었습니까?

해결책

You could pass it to a variable and use this notation:

Sub MrJ()
Dim rng As Range
Dim lastrow As Integer
lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = Range("Stock4")
rng.AutoFill Destination:=Range(rng.Address, Cells(lastrow, rng.Column))
End Sub
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top