converting references from static to dynamic

Luke777

Board Regular
Joined
Aug 10, 2020
Messages
246
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I've been using the below to place a formula in V2 and autofill it to my last Column.

VBA Code:
With ws
.Cells(1, lCol).Offset(1, 0).FormulaR1C1 = _

"=IF(RC[-4]-RC[-6]<0, ""-"" & TEXT(ABS(RC[-4]-RC[-6]),""d hh:mm:ss""), RC[-4]-RC[-6])"

.Cells(1, lCol).Offset(1, 0).AutoFill Destination:=.Range("V2:V" & lRow) 

.Range("V2:V" & lRow).NumberFormat = "[$-x-systime]d h:mm:ss AM/PM"
end ws

.Cells(1,lCol) refers to V1, the offset makes it V2. This part is already dynamic, but I'm not sure how I'd go about using the same process for
VBA Code:
.Range("V2:V" & lRow)

Is it going to be something like
VBA Code:
.Range(cells(1,lCol).offset(1,0) &":" &cells(lRow,lCol))
? No idea how close that is

Thanks for any help
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You do not need to use autofill in VBA, if you have already calculated the last row you need to go down to. Just apply the formula to the whole range at once, i.e.
VBA Code:
.Range("V2:V" & lRow).FormulaR1C1 = _
    "=IF(RC[-4]-RC[-6]<0, ""-"" & TEXT(ABS(RC[-4]-RC[-6]),""d hh:mm:ss""), RC[-4]-RC[-6])"
 
Upvote 1
You do not need to use autofill in VBA, if you have already calculated the last row you need to go down to. Just apply the formula to the whole range at once, i.e.
VBA Code:
.Range("V2:V" & lRow).FormulaR1C1 = _
    "=IF(RC[-4]-RC[-6]<0, ""-"" & TEXT(ABS(RC[-4]-RC[-6]),""d hh:mm:ss""), RC[-4]-RC[-6])"
ahh okay, that will make things simpler, but how do I convert the below
.Range("V2:V" & lRow)
so that it's dynamic? i.e. changes before what I'm doing might make V no longer the column that I'm using.

I've already got the lCol value, I'm just not sure how to implement it in this scenario
 
Upvote 0
You can use "Cells" in place of "Range", i.e. to go from row 2 down to the last row ("lRow") in the lCol column, you can use this:
VBA Code:
.Range(.Cells(2, lCol), .Cells(lRow, lCol)).FormulaR1C1 = _
 
Upvote 1
Solution
You can use "Cells" in place of "Range", i.e. to go from row 2 down to the last row ("lRow") in the lCol column, you can use this:
VBA Code:
.Range(.Cells(2, lCol), .Cells(lRow, lCol)).FormulaR1C1 = _
Thank you! This was the solution I was wondering about in my first post :) I'm glad I wasn't too far off! But thank you for getting it over the line
 
Upvote 0
You are welcome!

That is one reason why I usually prefer using "Cells" over "Range" when dealing with variable column references.
"Cells" can accept the index number of the column, where "Range" requires the column letter, which is harder to get.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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