动态求和范围
Dynamic Summing Range
目前我有一份医疗差价单-sheet,其中包含我们服务过的客户名单。我们有 8 个不同的临床类别,用不同的首字母缩写词表示 - HV、SV、CV、WV、CC、OV、TS 和 GS。
一个客户可以接受多种疗法,即 HV、SV、CV - 在后台我们有一个计数器机制,可以将这些记录中的每一个增加 1.The 用于此计数器的公式是:
=(LEN('Parent Sheet'!F25)-LEN(SUBSTITUTE('Parent Sheet'!F25,'Parent Sheet'!$P,"")))/LEN('Parent Sheet'!$P)
然后在 sheet 的底部,我们有一个总和,将那一周发生的所有治疗加起来。
现在棘手的部分是我们在这个 sheet 中有将近一年的数据,但求和公式设置为:SUM(COLUMN 6: COLUMN 53)
但由于需要增加条目超出这个限制,我们必须调整求和公式。我们有 300 个 SUM 公式,将 8 个标准项目中的每一个相加并将它们分配给 HV、SV、SC、WV 等计数器。
我们是否必须一项一项地手动调整,或者有更简单的方法吗?
非常感谢!
拥有 300 个 sum
公式并不是什么好主意。
Name your data range and include that inside the SUM
formula. So each time the NAMED data range expands, the sum gets calculated based on that. Here's how to create a dynamic named rnage.
抱歉,我刚刚看到你的评论。以下是 simple/crude VBA 片段。
Range("B3:F12") is rangeValue; Range("C18") is rngTotal.
Option Explicit
Sub SumAll()
Dim WS As Worksheet
Dim rngSum As Range
Dim rngData As Range
Dim rowCount As Integer
Dim colCount As Integer
Dim i As Integer
Dim varSum As Variant
'assuming that your said mechanism increases the data range by 1 row
Set WS = ThisWorkbook.Sheets("Sheet2")
Set rngData = WS.Range("valueRange")
Set rngSum = WS.Range("rngTotal")
colCount = rngData.Columns.Count
'to take the newly added row (by your internal mechanism) into consideration
rowCount = rngData.Rows.Count + 1
ReDim varSum(0 To colCount)
For i = 0 To UBound(varSum, 1)
varSum(i) = Application.Sum(rngData.Resize(rowCount, 1).Offset(, i))
Next i
'transpose variant array with totals to sheet range
rngSum.Resize(colCount, 1).Value = Application.Transpose(varSum)
'release objects in the memory
Set rngSum = Nothing
Set rngData = Nothing
Set WS = Nothing
Set varSum = Nothing
End Sub
屏幕:
您可以按照 bonCodigo 的建议使用命名范围,或者您可以使用查找和替换,或者您可以在数据范围内插入列,Excel 会自动为您更新公式。
对我来说,我认为您应该稍微更改 sheet 布局,创建一个用户定义函数 (UDF) 并更改求和行中的公式以高效 row/column 添加(使使用 Excel 的公式填充)。唯一的问题是您需要将其另存为 Macro-Enabled 文件。
您需要在公式中更改的是利用 $
来限制公式填充时列和行的更改。
为了举例说明,请考虑:
假设第一个数据从第 6 行开始,不超过第 15 行(您可以使用顶部另一个数据间隙的想法)。将 Sum 行标题更改为以缩写开头,然后创建如下 UDF:
Option Explicit
' The oRngType refers to a cell where the abbreviation is stored
' The oRngCount refers to cells that the abbreviation is to be counted
' Say "HV" is stored in $C16, and the cells to count for HV is D:D,
' then the sum of HV for that date (D16) is calculated by formula
' `=CountType($C16, D:D)`
Function CountType(ByRef oRngType As Range, ByRef oRngCount) As Long
Dim oRngVal As Variant, oVal As Variant, oTmp As Variant, sLookFor As String, count As Long
sLookFor = Left(oRngType.Value, 2)
oRngVal = oRngCount.Value ' Load all the values onto memory
count = 0
For Each oVal In oRngVal
If Not IsEmpty(oVal) Then
For Each oTmp In Split(oVal, ",")
If InStr(1, oTmp, sLookFor, vbTextCompare) > 0 Then count = count + 1
Next
End If
Next
CountType = count
End Function
sheet中的公式:
要求和的列固定为第 6 到 15 行,查找类型固定为 C 列
D16 | =CountType($C16,D:D)
D17 | =CountType($C17,D:D)
...
E16 | =CountType($C16,E:E)
E17 | =CountType($C17,E:E)
我创建 UDF 的方式是在单元格范围(第二个参数)中查找和计算单元格值(第一个参数)的出现次数。因此,您可以使用它来计算大范围细胞(G 列)的一种治疗类型。
现在,如果您在 F 后添加许多列,您只需使用自动填充,相应的行和列就会出现。
您还可以创建另一个 VBA Sub 来为您添加行、列和公式,但那是另一个问题。
目前我有一份医疗差价单-sheet,其中包含我们服务过的客户名单。我们有 8 个不同的临床类别,用不同的首字母缩写词表示 - HV、SV、CV、WV、CC、OV、TS 和 GS。
一个客户可以接受多种疗法,即 HV、SV、CV - 在后台我们有一个计数器机制,可以将这些记录中的每一个增加 1.The 用于此计数器的公式是:
=(LEN('Parent Sheet'!F25)-LEN(SUBSTITUTE('Parent Sheet'!F25,'Parent Sheet'!$P,"")))/LEN('Parent Sheet'!$P)
然后在 sheet 的底部,我们有一个总和,将那一周发生的所有治疗加起来。
现在棘手的部分是我们在这个 sheet 中有将近一年的数据,但求和公式设置为:SUM(COLUMN 6: COLUMN 53)
但由于需要增加条目超出这个限制,我们必须调整求和公式。我们有 300 个 SUM 公式,将 8 个标准项目中的每一个相加并将它们分配给 HV、SV、SC、WV 等计数器。
我们是否必须一项一项地手动调整,或者有更简单的方法吗?
非常感谢!
拥有 300 个 sum
公式并不是什么好主意。
Name your data range and include that inside the SUM
formula. So each time the NAMED data range expands, the sum gets calculated based on that. Here's how to create a dynamic named rnage.
抱歉,我刚刚看到你的评论。以下是 simple/crude VBA 片段。
Range("B3:F12") is rangeValue; Range("C18") is rngTotal.
Option Explicit
Sub SumAll()
Dim WS As Worksheet
Dim rngSum As Range
Dim rngData As Range
Dim rowCount As Integer
Dim colCount As Integer
Dim i As Integer
Dim varSum As Variant
'assuming that your said mechanism increases the data range by 1 row
Set WS = ThisWorkbook.Sheets("Sheet2")
Set rngData = WS.Range("valueRange")
Set rngSum = WS.Range("rngTotal")
colCount = rngData.Columns.Count
'to take the newly added row (by your internal mechanism) into consideration
rowCount = rngData.Rows.Count + 1
ReDim varSum(0 To colCount)
For i = 0 To UBound(varSum, 1)
varSum(i) = Application.Sum(rngData.Resize(rowCount, 1).Offset(, i))
Next i
'transpose variant array with totals to sheet range
rngSum.Resize(colCount, 1).Value = Application.Transpose(varSum)
'release objects in the memory
Set rngSum = Nothing
Set rngData = Nothing
Set WS = Nothing
Set varSum = Nothing
End Sub
屏幕:
您可以按照 bonCodigo 的建议使用命名范围,或者您可以使用查找和替换,或者您可以在数据范围内插入列,Excel 会自动为您更新公式。
对我来说,我认为您应该稍微更改 sheet 布局,创建一个用户定义函数 (UDF) 并更改求和行中的公式以高效 row/column 添加(使使用 Excel 的公式填充)。唯一的问题是您需要将其另存为 Macro-Enabled 文件。
您需要在公式中更改的是利用 $
来限制公式填充时列和行的更改。
为了举例说明,请考虑:
假设第一个数据从第 6 行开始,不超过第 15 行(您可以使用顶部另一个数据间隙的想法)。将 Sum 行标题更改为以缩写开头,然后创建如下 UDF:
Option Explicit
' The oRngType refers to a cell where the abbreviation is stored
' The oRngCount refers to cells that the abbreviation is to be counted
' Say "HV" is stored in $C16, and the cells to count for HV is D:D,
' then the sum of HV for that date (D16) is calculated by formula
' `=CountType($C16, D:D)`
Function CountType(ByRef oRngType As Range, ByRef oRngCount) As Long
Dim oRngVal As Variant, oVal As Variant, oTmp As Variant, sLookFor As String, count As Long
sLookFor = Left(oRngType.Value, 2)
oRngVal = oRngCount.Value ' Load all the values onto memory
count = 0
For Each oVal In oRngVal
If Not IsEmpty(oVal) Then
For Each oTmp In Split(oVal, ",")
If InStr(1, oTmp, sLookFor, vbTextCompare) > 0 Then count = count + 1
Next
End If
Next
CountType = count
End Function
sheet中的公式:
要求和的列固定为第 6 到 15 行,查找类型固定为 C 列
D16 | =CountType($C16,D:D)
D17 | =CountType($C17,D:D)
...
E16 | =CountType($C16,E:E)
E17 | =CountType($C17,E:E)
我创建 UDF 的方式是在单元格范围(第二个参数)中查找和计算单元格值(第一个参数)的出现次数。因此,您可以使用它来计算大范围细胞(G 列)的一种治疗类型。
现在,如果您在 F 后添加许多列,您只需使用自动填充,相应的行和列就会出现。
您还可以创建另一个 VBA Sub 来为您添加行、列和公式,但那是另一个问题。