Excel vba Range & formula Question

Biz

Well-known Member
Joined
May 18, 2009
Messages
1,773
Office Version
  1. 2021
Platform
  1. Windows
Dear All,

I am trying to make and ranges formulas that are not hardcode. fLR is last row in column A.

Instead of using Range("B67:I67") I am trying make it dynamic using
Cells(fLR + 1, 2, Cells(fLR + 1, 9)) but not working. Formula should be
SUM(B50:B66).

How can I make formula and range more variable?


Code:
Sub Test()
Dim fLR As Long
fLR = Cells(Rows.Count, "A").End(xlUp).Row
'With Range("B67:I67")before
'Apply Subtotals
With Cells(fLR + 1, 2, Cells(fLR + 1, 9))
    .FormulaR1C1 = "=SUM(R[-17]C:R[-1]C)"
    .Font.Bold = True
End Sub

End With

Biz
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi njimack,

Thank you but how about formula

Should be Sum formula in vba code below. SUM(B50:B66)
Code:
.FormulaR1C1 = "=SUM(R[-17]C:R[-1]C)"

Biz
 
Upvote 0
Maybe something like this (I'm not a fan of R1C1 notation):

With Range(Cells(fLR + 1, 2), Cells(fLR + 1, 9))
.Formula = "SUM(B" & fLR - 17 & ":B" & fLR-1 & ")"
 
Upvote 0
Mate you mean

Code:
.Formula = "=SUM(B" & fLR - 16 & ":B" & fLR & ")"

I solved it another way.
Step 1 Define a ranged name drTitle As Range as A1
Step 2 Defined Dim aRangeDiff As Long, drTitle As Range
Step 3 Created a formula
Code:
.FormulaR1C1 = "=SUM(R[-" & aRangeDiff & "]C:R[-1]C)"

This is good I am learning another way.

Biz
 
Upvote 0
Sorry

Step 1 Define a ranged name drTitle As Range as B50 is the first cell in addition range.

 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,275
Members
452,902
Latest member
Knuddeluff

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