Using Variables for R1C1 Index

Gerald42

New Member
Joined
Jul 27, 2007
Messages
12
I am trying to use the custom variable "Index" so that I can use this macro on arbitrarily long data sets.

I'm trying to directly add this variable in the FormulaR1C1 notation I used below. Is there some way of referencing it? Below I tried concatenating it with the & sign but that does not work.

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 7/30/2007 by bendej3
'

'
LastRowColA = Range("A65536").End(xlUp).Row
Index = -LastRowColA + 1
Range("A" & (LastRowColA + 1)).Select
ActiveCell.FormulaR1C1 = "Sum"
Range("C" & (LastRowColA + 1)).Select
ActiveCell.FormulaR1C1 = _
"=SUMIF(R["&Index&"]C[-1]:R[-1]C[-1],""All Parameters"",R["&Index&"]C:R[-1]C)"
End Sub

Thanks!
 

Excel Facts

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

Lewiy

Well-known Member
Joined
Jan 5, 2007
Messages
4,284
Couple of things. Firstly, you need to put a space between a string and an “&” sign, so instead of variable&”string”, you would put variable & “string”. Other than that your formula was fine.
Secondly, you should avoid using “Index” as a variable name because it could be confused with the Index property.
Finally, you don’t usually need to use “Select” commands. This will ultimately slow down your code.
Try this (hopefully you can see what I’ve done based on the above nores:
Code:
Sub Macro1()
Dim myIndex As Long
myIndex = Range("A65536").End(xlUp).Row + 1
Range("A" & myIndex) = "Sum"
Range("C" & myIndex).FormulaR1C1 = "=SUMIF(R[" & myIndex & "]C[-1]:R[-1]C[-1],""All Parameters"",R[" & myIndex & "]C:R[-1]C)"
End Sub
 

Forum statistics

Threads
1,181,055
Messages
5,927,862
Members
436,573
Latest member
CMR237

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
Top