Using a Variable in a Formula in R1C1 Style in VBA

rschmidt46

New Member
Joined
Aug 24, 2015
Messages
46
Office Version
  1. 2010
Platform
  1. Windows
I cannot get the following code to work and I do not know why:

Sub Count_Records()
Dim LCol, LRow, Row_Diff, X As Integer
LRow = [A1].End(xlDown).Row + 1
Row_Diff = 2 - LRow
Cells(LRow, 1).FormulaR1C1 = "=COUNTA(R[" & [Row_Diff] & "]C:R[-1]C"
End

I receive a weekly report with a different number of rows most times. I'm trying to enter a "COUNTA" formula in the row below the last one with data using VBA. The formula needs to reflect the difference between row 2 and the last row with data (variable "LRow"). I thought defining the difference with another variable "Row_Diff" would work but the line of code errors out. Can someone spot what's wrong with the syntax or if a variable cannot be used like this?
 

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.
There is no need for the Row_Diff variable, you can use
VBA Code:
Cells(LRow, 1).FormulaR1C1 = "=COUNTA(R2C:R[-1]C)"
 
Upvote 0
Solution
Fluff,
Wow, that was simple, although I apparently don't know how R1C1 works and rarely use it! Seems like a better way of coding many of the things I regularly encounter.
Thanks,
Randy
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

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