Using VBA to expand a formula each time it runs

cimajc17

New Member
Joined
Jun 10, 2015
Messages
15
HI,
I am trying to expand a formula each time this macro runs but i am having trouble extending the range of my function as more data is added,

The formula is this
Range ("E2').select
ActiveCell.FormulaR1C1 = "=COUNTIF(R2C2:R3549C2,1)"

But i want the formula to work using the last filled row in column B as i add more data each time i run the macro

I have tried this but it does not work

startRow = 1
col = 1
lastRowHistogram2 = Findlastrow(col)

Range("E2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(R2C2:RlastRowHistogram2C2,1)"
with the function find last row as follows
Function Findlastrow(col) As Long
Findlastrow = Cells(Rows.Count, col).End(xlUp).row
End Function
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try this in your macro:

Define the last row first and then have the formula reflect that last row

Code:
LastRow = Range("B" & Rows.Count).End(xlUp).Row
Range("E2").Formula = "=Countif(B2:B" & LastRow & ",1)"

Test in a copy!
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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