syntax for SUM formula in VBA

MargAtCanon

New Member
Joined
Aug 11, 2009
Messages
23
Hi,
I'm writing some vba code where I want a formula to be pasted into a range of cells. I'm OK with other formulas because I copy a coleagues stuff. However, I am having trouble with this SUM because I don't really understand the syntax.
Can someone please help me?
Thank you.
Code:
ActiveCell.FormulaR1C1 = "=SUM(RC" & Area1SalesRepCol: OtherSalesRepCol ")"
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I think if the variables are column numbers rather than letters, this ought to work
Code:
ActiveCell.FormulaR1C1 = "=SUM(RC" & Area1SalesRepCol & ":RC" & OtherSalesRepCol ")"
 
Upvote 0
What end-result formula (not VBA) do you want?

What are the values of Area1SalesRepCol an OtherSalesRepCol ? Are they column letters e.g. "A" and "C" or are they ranges e.g. Range("A:A") and Range("C:C") or integers e.g. 1 and 3.?

Depending on what end-result formula you want and the values of the two variables, that will determine the syntax of the VBA statement.
 
Upvote 0
thank you both.
I tried the suggestion from Weaver and i get the Compile error: Expected end of statement.

To answer AlphaFrog's question, my formula should look like this once it's pasted into the worksheet.
=SUM(AH2:AO2)
 
Upvote 0
To answer AlphaFrog's question, my formula should look like this once it's pasted into the worksheet.
=SUM(AH2:AO2)
What are the values of Area1SalesRepCol and OtherSalesRepCol ? Are they column letters e.g. "A" and "C" or are they ranges e.g. Range("A:A") and Range("C:C") or integers e.g. 1 and 3.?
 
Upvote 0
Code:
ActiveCell.FormulaR1C1 = "=SUM(RC" & Area1SalesRepCol & ":RC" & OtherSalesRepCol [COLOR="Red"]&[/COLOR] ")"
 
Upvote 0
Thank you mikerickson.

That has worked. I had experimented with the & and " but couldn't quite get the combination right.

I know this is very basic, but can you explain the ampersand's role? I have always thought it acts to join things. Can you recommend some reading so I can understand the very basics of VBA? In the course I have done and in the other texts I have read, there is an assumption that this is already known. Trying to pick this up in my 50s, I have a gap in my knowledge that is very frustrating.
 
Upvote 0
AlphaFrog,

The values of Area1SalesRepCol and OtherSalesRepCol are column letters.
They are identified earlier in the code by
Code:
Area1SalesRepCol = colIndex(backorderWS, Range("Area1_SalesRep"))
the named range Area1_SalesRep is a name in a cell in another worksheet.
Does this explain?
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,448
Members
452,915
Latest member
hannnahheileen

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