DarkSilence1979
New Member
- Joined
- Oct 28, 2019
- Messages
- 8
Hello everyone!
First time post here and I am hoping someone will be able to answer a couple of issues I have been having with just starting out with VBA on excel. I have a spreadsheet that will have new info every week so the columns will stay the same, but the number of rows will vary. After a lot of searching on google, I was able to figure out how to sum the columns with varying rows by using the following VBA:
Dim myRangeD
Dim myRangeE
Dim myRangeF
Range("G1").Select
ActiveCell.FormulaR1C1 = "40"
Cells.Select
Cells.EntireColumn.AutoFit
Range("D2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "General"
Set endRE = ActiveSheet.Range("E2").End(xlDown)
Set myRangeE = ActiveSheet.Range("E2", endRE)
endRE.Offset(2, 0) = WorksheetFunction.Sum(myRangeE)
Set endRF = ActiveSheet.Range("F2").End(xlDown)
Set myRangeF = ActiveSheet.Range("F2", endRF)
endRF.Offset(2, 0) = WorksheetFunction.Sum(myRangeF)
Set endRD = ActiveSheet.Range("D2").End(xlDown)
Set myRangeD = ActiveSheet.Range("D2", endRD)
endRD.Offset(2, 0) = WorksheetFunction.Sum(myRangeD)
It works great, the only problem is after the macro is ran, the WorksheetFunction.Sum(myRangeE), (myRangeF), and (myRangeD) all input a value into the cell. I need it to keep it as a formula since this worksheet is dynamic and users change values in the columns and are expecting a new calculated value in these cells. How do I force VBA to keep the formula instead of calculating the value in the background? Thanks to everyone for their time.
Ryan
First time post here and I am hoping someone will be able to answer a couple of issues I have been having with just starting out with VBA on excel. I have a spreadsheet that will have new info every week so the columns will stay the same, but the number of rows will vary. After a lot of searching on google, I was able to figure out how to sum the columns with varying rows by using the following VBA:
Dim myRangeD
Dim myRangeE
Dim myRangeF
Range("G1").Select
ActiveCell.FormulaR1C1 = "40"
Cells.Select
Cells.EntireColumn.AutoFit
Range("D2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.NumberFormat = "General"
Set endRE = ActiveSheet.Range("E2").End(xlDown)
Set myRangeE = ActiveSheet.Range("E2", endRE)
endRE.Offset(2, 0) = WorksheetFunction.Sum(myRangeE)
Set endRF = ActiveSheet.Range("F2").End(xlDown)
Set myRangeF = ActiveSheet.Range("F2", endRF)
endRF.Offset(2, 0) = WorksheetFunction.Sum(myRangeF)
Set endRD = ActiveSheet.Range("D2").End(xlDown)
Set myRangeD = ActiveSheet.Range("D2", endRD)
endRD.Offset(2, 0) = WorksheetFunction.Sum(myRangeD)
It works great, the only problem is after the macro is ran, the WorksheetFunction.Sum(myRangeE), (myRangeF), and (myRangeD) all input a value into the cell. I need it to keep it as a formula since this worksheet is dynamic and users change values in the columns and are expecting a new calculated value in these cells. How do I force VBA to keep the formula instead of calculating the value in the background? Thanks to everyone for their time.
Ryan