GeekMaster2000
New Member
- Joined
- Nov 22, 2021
- Messages
- 2
- Office Version
- 365
- Platform
- Windows
So, I have been plowing through the internet and haven't been able to find an answer to specifically help with this case.
I am building a relatively elaborate macro for my work and at one point I need to be able to insert a subtotal (sum) function when the cell contains a value (in this case, it will specifically always be the word "Total", if that helps) and the value in that cell needs to be replaced with the formula. In other words:
I have the following table:
Now, every time that in the column with the header "AJE" it says "Total", I want that cell to be replaced with the following formula => FormulaR1C1 "= SUBTOTAL(9,R9C5:R[-2]C) ". Or in other words, I want the formula to be a subtotal from the cell E9 until two cells above the active cell that has total written in it.
Then, I would like that formula to be copied and pasted (as a formula only) in certain other cells of the same row.
Then, I would like for this to be looped until E500 (since AJE is in column E) for each cell that contains Total.
What I have written at this point (which I know is wrong, but at least shows my intention) is the following (FYI, I left the 'Else' blank, because I don't want anything to happen if the cell is blank, just for it to run through the loop in the desired range):
Dim rngc As Range, c As Range
Set rngc = Range("E9:E500")
For Each c In rngc
If Not IsEmpty(cell) Then
Active.cell.FormulaR1C1 "= SUBTOTAL(9,R9C5:R[-2]C) "
Selection.Copy
ActiveCell.Offset(0, -1).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(0, 2).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(0, 2).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Else
End If
Next c
The offset references are what need to have the formula pasted in there. This table can have much more than two totals, but will never go beyond the E500 range.
Any help is GREATLY appreciate it, have been trying to solve this for a while now!
I am building a relatively elaborate macro for my work and at one point I need to be able to insert a subtotal (sum) function when the cell contains a value (in this case, it will specifically always be the word "Total", if that helps) and the value in that cell needs to be replaced with the formula. In other words:
I have the following table:
Now, every time that in the column with the header "AJE" it says "Total", I want that cell to be replaced with the following formula => FormulaR1C1 "= SUBTOTAL(9,R9C5:R[-2]C) ". Or in other words, I want the formula to be a subtotal from the cell E9 until two cells above the active cell that has total written in it.
Then, I would like that formula to be copied and pasted (as a formula only) in certain other cells of the same row.
Then, I would like for this to be looped until E500 (since AJE is in column E) for each cell that contains Total.
What I have written at this point (which I know is wrong, but at least shows my intention) is the following (FYI, I left the 'Else' blank, because I don't want anything to happen if the cell is blank, just for it to run through the loop in the desired range):
Dim rngc As Range, c As Range
Set rngc = Range("E9:E500")
For Each c In rngc
If Not IsEmpty(cell) Then
Active.cell.FormulaR1C1 "= SUBTOTAL(9,R9C5:R[-2]C) "
Selection.Copy
ActiveCell.Offset(0, -1).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(0, 2).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(0, 2).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveCell.Offset(0, 1).Select
Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Else
End If
Next c
The offset references are what need to have the formula pasted in there. This table can have much more than two totals, but will never go beyond the E500 range.
Any help is GREATLY appreciate it, have been trying to solve this for a while now!