Need Help with VBA Code for Sum Formula for Variable Cells

Chili SHot

New Member
Joined
Jun 1, 2022
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Column AColumn B
1
2
3
=700-sum(A2:A4)

The code below is what I want, but I would like to modify the code to use 700 - sum (the column B) and show the result in Column A. Please advise.


Sub MySum()

Dim cr As Long
Dim lr As Long

' Get current row of activecell
cr = ActiveCell.Row

' Get first row of range
fr = ActiveCell.Offset(-1, 0).End(xlUp).Row

' Enter sum formula into activecell
ActiveCell.FormulaR1C1 = "=SUM(R[-" & cr - fr & "]C:R[-1]C)"

End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Welcome to the Board!

What row will your data in column B start on? Row 1? Row 2?
Do you just want it to sum down to the very last row with data in column B, and then put the sum in column, on the next row?
Will there always be just one set of data in column B we will be summing?
 
Upvote 0
If my assumptions are correct, and the first row with data is row 2, then you could use this code:
VBA Code:
Sub MyInsertSum()

    Dim lr As Long
    
'   Find last row in column B with data
    lr = Cells(Rows.Count, "B").End(xlUp).Row
    
'   Populate column A with formula
    Range("A" & lr + 1).Formula = "=700-SUM(B2:B" & lr & ")"
    
End Sub
If the data starts on row 1, just change "B2" to "B1" in the formula.
 
Upvote 0
Solution
If my assumptions are correct, and the first row with data is row 2, then you could use this code:
VBA Code:
Sub MyInsertSum()

    Dim lr As Long
   
'   Find last row in column B with data
    lr = Cells(Rows.Count, "B").End(xlUp).Row
   
'   Populate column A with formula
    Range("A" & lr + 1).Formula = "=700-SUM(B2:B" & lr & ")"
   
End Sub
If the data starts on row 1, just change "B2" to "B1" in the formula.
Thank you @Joe4!
 
Upvote 0
You are welcome.
 
Upvote 0
xxxx (Row1)Column AColumn B
row 21$ -$ -
row31$ -$ -
1$ -$ -
1$ -$ -
1$ -$ -
1$ -$ -
1$ -$ -
2$ -$ -
2$ -$ -
1$ -$ -
1$ -$ -
Supplement=700-sum( row2:Row13)
xxx Row14
 
Upvote 0
1654112310814.png


This is what I am trying to do. I tried to modify the columns in your code it is not working. Please see below.

Sub MyInsertSum()

Dim lr As Long

' Find last row in column H with data
lr = Cells(Rows.Count, "H").End(xlUp).Row

' Populate column G with formula
Range("G" & lr + 1).Formula = "=700-SUM(H2:H" & lr & ")"

End Sub
 
Upvote 0
View attachment 66092

This is what I am trying to do. I tried to modify the columns in your code it is not working. Please see below.

Sub MyInsertSum()

Dim lr As Long

' Find last row in column H with data
lr = Cells(Rows.Count, "H").End(xlUp).Row

' Populate column G with formula
Range("G" & lr + 1).Formula = "=700-SUM(H2:H" & lr & ")"

End Sub
Please explain in English exactly what you are trying to do in this instance, as it does not look the same as your original question.
 
Upvote 0
@Joe4 I got it. Thanks a lot! I fix it.

Sub MyInsertSum()

Dim lr As Long

' Find last row in column H with data
lr = Cells(Rows.Count, "H").End(xlUp).Row

' Populate column G with formula
Range("G" & lr).Formula = "=700-SUM(H2:H" & lr - 1 & ")"

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

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