Share via

using a variable in a VBA formula

Anonymous
2010-06-14T07:59:18+00:00

Can someone explain to me what the difference is in the following FormulaR1C1 lines? "sumcolumn" is Dim as Integer and the active cell in this example is V2. I can give you the full Macro code if it would help.

        sumcolumn = (ActiveCell.Column - 1) * -1

'        ActiveCell.FormulaR1C1 = "=SUM(RC[sumcolumn]:RC[-1])"

'        ActiveCell.FormulaR1C1 = "=SUM(RC[-21]:RC[-1])"

The second FormulaR1C1 line returns the correct response, but I want a "floating" sum, based on what has been input, such that the active cell may be in column A to column AA...

I am not understanding the difference.

Fr. Frank

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2010-06-14T08:25:44+00:00

If you want to embed a variable value into a formula, remember that the formula is just a text string, so you have to concatenate that value with the hard-coded text,like so

ActiveCell.FormulaR1C1 = "=SUM(RC[" & sumcolumn & "]:RC[-1])"

--

HTH

Bob

<Fr. Frank> wrote in message news:*** Email address is removed for privacy *** .com...

Can someone explain to me what the difference is in the following FormulaR1C1 lines? "sumcolumn" is Dim as Integer and the active cell in this example is V2. I can give you the full Macro code if it would help.

        sumcolumn = (ActiveCell.Column - 1) * -1

'        ActiveCell.FormulaR1C1 = "=SUM(RC[sumcolumn]:RC[-1])"

'      &n bsp; ActiveCell.FormulaR1C1 = "=SUM(RC[-21]:RC[-1])"

The second FormulaR1C1 line returns the correct response, but I want a "floating" sum, based on what has been input, such that the active cell may be in column A to column AA...

I am not understanding the difference.

Fr. Frank

Was this answer helpful?

4 people found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful