[英]How do I edit my sum formula to select a dynamic range of cells?
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 我记录了一个宏,确保选择了使用相对参考,但是当运行宏时,总和功能总是选择单元格上方的8个单元格,即使我使用Ctrl
+
Shift +
箭头向上记录它来选择所有非 - 正上方的空白细胞 :
:
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 任何意见或建议都将是一个巨大的帮助
Public Function sumAbove(ByVal CellSelected As Range) As Double
Dim FirstSelected As Range
Dim TopMostNonBlankCell As Range
Set FirstSelected = CellSelected.Cells(1) 'if user inputs more than one cell, we use only the first
If FirstSelected.Offset(-1).Value2 = vbNullString Then 'we check, if the value above FirstSelected is blank
Set TopMostNonBlankCell = FirstSelected
Else 'If it is not blank, we use CTRL+SHIFT+UP
Set TopMostNonBlankCell = FirstSelected.End(xlUp)
End If
'Some error handling should be put above, to handle if the sumAbove is used in first row.
sumAbove = WorksheetFunction.Sum(Range(TopMostNonBlankCell, CellSelected).Value2)
End Function
To incorporate this into your code, try to use this: 要将其合并到您的代码中,请尝试使用:
ActiveCell.FormulaR1C1 = "=SUM(" & ActiveCell.Offset(-1).Address(ReferenceStyle:=xlR1C1) & ":" & ActiveCell.Offset(-1).End(xlUp).Address(ReferenceStyle:=xlR1C1) & ")"
To explain: 解释:
ActiveCell.Formula = "=SUM(" & ActiveCell.Offset(-1).Address & ":" & ActiveCell.Offset(-1).End(xlUp).Address & ")"
. ActiveCell.Formula = "=SUM(" & ActiveCell.Offset(-1).Address & ":" & ActiveCell.Offset(-1).End(xlUp).Address & ")"
。 The result is the same. Set ActiveCellMinusRow = ActiveCell.Offset(-1)
and use it in the code I proposed. Set ActiveCellMinusRow = ActiveCell.Offset(-1)
并在我提出的代码中使用它。 This is the solution I used as it covers off all the options I can think of in the data sets it will be used on. 这是我使用的解决方案,因为它涵盖了我将在其将使用的数据集中考虑的所有选项。
If ActiveCell.Offset(-2) = "" Then
ActiveCell.Formula = "=SUM(" & ActiveCell.Offset(-1).Address & ":" & ActiveCell.Offset(-1).Address & ")"
Else
ActiveCell.Formula = "=SUM(" & ActiveCell.Offset(-1).Address & ":" & ActiveCell.Offset(-1).End(xlUp).Address & ")"
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.