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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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,215,455
Messages
6,124,937
Members
449,196
Latest member
Maxkapoor

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