我的问题是发生了什么here和here的混合,但它们都不是很好。我有一个奇怪的数据集,它的结构更像一个层次结构,而不是一系列纯粹的数据点。事情是这样的:VBA Excel - 将小计公式插入特定行
Item Budget Sales GROUP - Cats 0 120 FY 13 Persian 0 0 FY 13 Maine Coon 12 0 FY 14 Maine Coon 50 0 FY 12 Tabby 1 0 FY 13 Tabby 1 0 FY 14 Tabby 2 0 FY 14 Alley 12 0 GROUP - Dogs 0 201 FY 14 Collie 20 0 FY 14 Lab 31 0 FY 13 Golden Retriever 12 0 FY 12 Golden Retriever 0 0 GROUP - Gold Fish 0 50 FY 14 Goldfish 100 0 FY 13 Clown Fish 20 0 Tanks Fees 150 0
我需要一个宏,可以整齐地标识该组线,然后求和组在它之下 - 没有捕捉下一组。换句话说,我需要猫预算项目来总结猫的预算。
所以宏需要识别带有“GROUP *”的行,然后向下搜索直到找到下一个“GROUP *”,然后将Cat和Dog之间的空间相加 - 最好作为函数 - 在“GROUP - Cats”行上。
我知道这一定是可能的,但我担心它对于我在VBA中的基本能力来说太复杂了。
编辑:最终产品将在预算列中简单地= SUM(B3:B9)的公式。然后B10(狗组)将有一个公式,例如= SUM(B11:B14)。对于黄金鱼:= SUM(B16:18)。
但是,由于每个数据集都是不断变化的(例如,本周可能会有20行而不是前一周的18行),我需要一个可以找到GROUP行之间空间的宏。
编辑2:我使用的是目前做类似的东西是什么我正在寻找的VBA - 它本质团体和折叠基础上的数字我的部分出现在Sales列:
Dim rStart As Range, r As Range
Dim lLastRow As Long, sColumn As String
Dim rColumn As Range
'### The Kittens everywhere! thing is just to make sure the last group has an end point
Range("C1").End(xlDown).Offset(1).Value = "Kittens everywhere!"
sColumn = "C"
With ActiveSheet.Outline
.AutomaticStyles = False
.SummaryRow = xlAbove
.SummaryColumn = xlRight
lLastRow = Cells(Rows.Count, sColumn).End(xlUp).Row
With ActiveSheet
Set rColumn = .Range(.Cells(1, sColumn), Cells(lLastRow, sColumn))
With rColumn
Set r = .Cells(1, 1)
Do Until r.Row > lLastRow
If rStart Is Nothing Then
If r.Value = "0" Then
Set rStart = r
End If
Else
If r.Value <> "0" Then
Range(rStart, r.Offset(-1, 0)).Rows.Group
Set rStart = Nothing
End If
End If
Set r = r.Offset(1, 0)
Loop
End With
End With
ActiveSheet.Outline.ShowLevels RowLevels:=1
End With
Range("C:C").Find("Kittens everywhere!").ClearContents
还有更多的宏 - 因为它也在做一些事情,像突出显示GROUP行 - 但我敢肯定,如果我可以将这个SUM功能的东西塞进该部分或不。
我不确定你到底在哪里感到困惑,但我添加了一些更多的信息。希望这会有所帮助。 :/ – PersonalSpace 2014-11-21 18:52:11
正式注意。我已经添加了一些我已有的内容,但我不确定它是否能适应当前的目标。 – PersonalSpace 2014-11-21 19:07:58