Put a Formula In A Cell

MikeG

Well-known Member
Joined
Jul 4, 2004
Messages
845
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Could anyone tell me how to put a formula in a cell using VBA?

So, for example, I have numeric values in cells B3, B5, and B7. I want cell B9, which starts off as blank, to be given the formula: SUM(B3:B7).

(Cells B4 and B6 are blank).

However, I would like the sum formula in cell B9 to be relative, so that if I delete what is now column A, the sum will still work without me having to change the macro command.

Thanks,

MikeG
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Thanks Peter.

That's great.

One enhancement I am looking for...

If I run the macro command, and then delete column A the SUM formula is now in A and refers to the cells in A - i.e. it is now =Sum(A3:A7), which is what I need.

However, if I rerun the macro, the command still puts the formula in "B9", because, of course, that is hard-coded, and the SUM formula still points to B3:B7, which are now blank because the values have since moved to column A.

So is there a way of making the command relative? I will tell the macro where the cell to put the formula goes using variables x and y, which will be updated if a column is deleted.

I think what I am looking for is something like:

Cells(x, y).Formula = "=SUM(B3:B7)"

But instead of the hardcoded B3:B7, is it something like "SUM(R-6C,R-2C)"?

Thanks,

Mike
 
Upvote 0
You can do that like this but it will still add the same formula

Code:
Range("B9").FormulaR1C1 = "=SUM(R[-6]C:R[-2]C)"
 
Upvote 0
You can do that like this but it will still add the same formula

Code:
Range("B9").FormulaR1C1 = "=SUM(R[-6]C:R[-2]C)"

Thanks. I see what you mean.

Is there a way to use a string type of formula?

So in the macro, define:

Column_to_Sum = "B"

Range(Column_To_Sum&"9").Formula = "=SUM(Column_To_Sum&"3,"&Column_To_Sum&"7)"

Mike
 
Upvote 0
Yes but you''ll end up with the same formula :)

Code:
Sub test()

Dim Column_To_Sum  As String
Column_To_Sum = "B"
Range(Column_To_Sum & 9).Formula = "=SUM(" & Column_To_Sum & 3 & ":" & Column_To_Sum & 7 & ")"
End Sub
 
Upvote 0
Yes but you''ll end up with the same formula :)

Code:
Sub test()

Dim Column_To_Sum  As String
Column_To_Sum = "B"
Range(Column_To_Sum & 9).Formula = "=SUM(" & Column_To_Sum & 3 & ":" & Column_To_Sum & 7 & ")"
End Sub

Thanks Peter. That helps - I'll use a Cell("Col","Formula_Cell") in the worksheet to track the column of the formula cell, read that into the macro, and set the Column_To_Sum variable accordingly.

Mike
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

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