大家好,
我一直在使用Stackoverflow几个月,通常,我设法找到解决我所有VBA挑战的解决方案:
我需要一些帮助来创建一个宏,它在行之间添加“avg”字的E列上的所有值。结果应显示在显示“Sum both”标签的单元格上。两个文本“avg”和“sum here”仅用于说明示例,“avg”可以替换为任何其他单词,“sum here”实际上应该是它上面的值的聚合。
真正的挑战是E列的范围数量是可变的,所以我想找到一个能够处理E列上“n”个范围的宏。
最后,D列上的值仅是“sum here”单元格上的预期值的示例。
这是我尝试过的:
Sub Macro1()
'
' Macro1 Macro
'
Dim sumhere As Range
Dim startingpoint As Range
Dim endingpoint As Range
'
Range("C17").Select
Selection.End(xlDown).Select
If ActiveCell = "avg" Then
ActiveCell.Offset(rowoffset:=0, columnoffset:=2).Select
Set sumhere = ActiveCell
Set startingpoint = ActiveCell.Offset(rowoffset:=-1, columnoffset:=0)
Selection.End(xlUp).Select
If (ActiveCell.Value) = "Sum here" Then
Set endingpoint = ActiveCell.Offset(rowoffset:=1, columnoffset:=0)
sumhere.Formula = "=sum(range(startingpoint:endingpoint)"
Else
Selection.End(xlUp).Select
If (ActiveCell.Value) = "Sum here" Then
Set endingpoint = ActiveCell.Offset(rowoffset:=1, columnoffset:=0)
sumhere.Formula = "=Sum(Range(startingpoint.adress:endingpoint.adress))"
Else: End If
End If
End If
End Sub
另外,正如您所看到的,我不知道如何使用变量定义范围。我最初的想法是将这些代码与某种“do while”或/和“for i = 1 to x”和“next i”相结合......但我看不出如何将它组合起来。
非常感谢你!
答案 0 :(得分:2)
仅使用公式,并且在每个小计行中仅提供列A的avg
(或任何文本)。
我已经给出了两个版本的公式 - 易失性版本(每次更改电子表格上的任何内容时更新)和非易失性版本(只有在需要时才更新)。
应该在第6行输入公式 - 将$E6
更改为您需要的行。
(易失性)
=SUM(OFFSET($E6,IFERROR(LOOKUP(2,1/($A$1:INDEX($A:$A,ROW()-1)<>""),ROW($A$1:INDEX($A:$A,ROW()-1))),0)-ROW()+1,,ROW()-1-IFERROR(LOOKUP(2,1/($A$1:INDEX($A:$A,ROW()-1)<>""),ROW($A$1:INDEX($A:$A,ROW()-1))),0)))
(非易失性):
=SUM(INDEX($E:$E,IFERROR(LOOKUP(2,1/($A$1:INDEX($A:$A,ROW()-1)<>""),ROW($A$1:INDEX($A:$A,ROW()-1))),0)+1):INDEX($E:$E,ROW()-1))
或者如果您不介意使用辅助列:
在单元格B6中:
=IFERROR(LOOKUP(2,1/($A$1:INDEX($A:$A,ROW()-1)<>""),ROW($A$1:INDEX($A:$A,ROW()-1))),0)
在E6中:(易失性)
=SUM(OFFSET($E6,$B6-ROW()+1,,ROW()-1-$B6))
或(非易失性):
=SUM(INDEX($E:$E,$B6):INDEX($E:$E,ROW()-1))
修改强> 我想如果你在VBA之后我会添加一个UDF来计算它。
在要显示小计的行中使用函数=AddSubTotal()
,或使用=AddSubTotal("pop",6)
使用“pop”而不是“avg”对F列(第6列)中的所有内容求和
Public Function AddSubTotal(Optional Delim As String = "avg", Optional ColNumber = 5) As Double
Dim rCaller As Range
Dim rPrevious As Range
Dim rSumRange As Range
Set rCaller = Application.Caller
With rCaller.Parent
Set rPrevious = .Range(.Cells(1, 1), .Cells(rCaller.Row - 1, 1)).Find(Delim, , , , , xlPrevious)
If Not rPrevious Is Nothing Then
Set rSumRange = rPrevious.Offset(1, ColNumber - 1).Resize(rCaller.Row - rPrevious.Row - 1)
Else
Set rSumRange = .Range(.Cells(1, ColNumber), .Cells(rCaller.Row - 1, ColNumber))
End If
End With
AddSubTotal = WorksheetFunction.Sum(rSumRange)
End Function
答案 1 :(得分:2)
以下VBA例程假设
avg
avg
(关键字)在宏您可以轻松修改此例程以执行这些值的平均值,并将这些结果放在例如D列中
以上任何一项都很容易修改
Option Explicit
Sub TotalSubRanges()
Dim vSrc As Variant, rSrc As Range
Dim dAdd As Double
Dim I As Long
Const sKey As String = "avg"
Set rSrc = Range(Cells(1, "C"), Cells(Rows.Count, "C").End(xlUp)).Resize(columnsize:=3)
vSrc = rSrc
'Do the "work" in a VBA array, as this will
' execute much faster than working directly
' on the worksheet
For I = 1 To UBound(vSrc, 1)
If vSrc(I, 1) = sKey Then
vSrc(I, 3) = dAdd
dAdd = 0
Else
If IsNumeric(vSrc(I, 3)) Then dAdd = dAdd + vSrc(I, 3)
End If
Next I
'write the results back to the worksheet
' and conditionally format the "sum" cells
With rSrc
.EntireColumn.Clear
.Value = vSrc
.Columns(3).AutoFit
.EntireColumn.ColumnWidth = .Columns(3).ColumnWidth
.FormatConditions.Delete
.FormatConditions.Add _
Type:=xlExpression, _
Formula1:="=" & .Item(1, 1).Address(False, True) & "=""" & sKey & """"
With .FormatConditions(1)
.Interior.ColorIndex = 6
End With
End With
End Sub
答案 2 :(得分:1)
当然你只需要:
Sub sums()
Dim i As Integer, j As Integer, k As Integer
j = Range("C1048576").End(xlUp).Row
k = 1
For i = 1 To j
If Range("C" & i).Value <> "" Then
Range("E" & i).Value = "=Sum(E" & k & ":E" & i - 1 & ")"
k = i + 1
End If
Next i
End Sub
答案 3 :(得分:1)
变化: 昏暗的起点作为范围 Dim endpoint As Range
要: Dim startingpoint As Variant 昏暗的终点作为变体
由于在公式中使用了起点和终点,因此您无法将它们定义为范围。