I recorded a macro making sure Use Relative References was selected but when running the macro, the sum function always selects the 8 cells above the cell where the total will appear, even though I recorded it using Ctrl+
Shift+
Arrow Up to select all non-blank cells directly above :
ActiveCell.FormulaR1C1 = "=SUM(R[-8]C:R[-1]C)
I've looked at the followings which are similar to what I want to achieve, but mine is in reverse, and can't figure out how to amend my code where it will sum every cell moving up the column until it hits a blank cell.
The objective is to be able to enter subtotals at different points in the worksheet where they sum ranges with differing numbers of cells in them.
This is what the entire macro looks like if it would help to see context:
Sub InsertTotal()
'
' InsertTotal Macro
' Insert blank rows, bold line and total amount
'
' Keyboard Shortcut: Ctrl+y
'
ActiveCell.Rows("1:2").EntireRow.Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.Offset(0, 7).Range("A1").Select
Selection.Font.Bold = True
ActiveCell.FormulaR1C1 = "=SUM(R[-8]C:R[-1]C)"
ActiveCell.Offset(-1, -7).Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlMedium
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
ActiveCell.Select
End Sub
Any comments or suggestions will be a huge help