Problem usng variable within RC[-1]

Ronanm

Board Regular
Joined
Nov 13, 2010
Messages
107
Hi

Can I use a variable within the RC addressing in a formula in VBA?

Selection.Value = "=sumIF(RC[-1]:R[lastrow]C,RC[-1]:R[lastrow]C,RC[8]:R[lastrow]C)"

I would have thought that the formula above would be fine!!!

Thanks

Ronan
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try

Code:
Selection.FormulaR1C1 = "=sumIF(RC[-1]:R[" & lastrow & "]C,RC[-1]:R[" & lastrow & "]C,RC[8]:R[" & lastrow & "]C)"
 
Upvote 0
Couple things:

Code:
Selection[B][COLOR=red].FormulaR1C1[/COLOR][/B] = "=sumIF(RC[-1]:R[[COLOR=red][B]" & lastrow & "[/B][/COLOR]]C,RC[-1]:R[[B][COLOR=red]" & lastrow & "[/COLOR][/B]]C,RC[8]:R[[COLOR=red][B]" & lastrow & "[/B][/COLOR]]C)"

Anothing thing to note is that the code the Macro Recorder gives you is extremely messy and can be cleaned up by removing most every "Select" statement. For example:

Code:
Range("A1").Select
Selection.Value = "Bob"

can be reduced to:

Code:
Range("A1") = "Bob"

Also, R1C1 formulas are incredibly hard to interpret. If you are able to translate it into an A1 style formula, you can use something like:

Code:
Range("B1").Formula = "=SUM(A1:A" & lastrow & ")"
 
Upvote 0
Actually... Below works, but one other issue. How do I make the Rows static, i.e. the $ signs? Do I need to &'sand them on as well?

Selection.FormulaR1C1 = "=sumIF(RC[-1]:R[" & lastrow & "]C[-1],RC[-1]:R[" & lastrow & "]C[-1],RC[9]:R[" & lastrow & "]C[9])"

Thanks

Ronan
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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