我希望使用内置函数在Excel VBA 没有的情况下为多个组提供子总计。即:https://msdn.microsoft.com/en-us/library/office/ff838166.aspx。折叠级别不是必需的。用户只需要查看Sub total和total之和的数据,因此在电子表格中不应该有任何使用“= SUBTOTAL”字符串函数的公式:SUBTOTAL more than 254 rows
应该存在类型1和类型2的总和分组。不需要总计。
示例:
数据的内容是概念化分组。如本例中针对脊椎动物和无脊椎动物所述,生产数据将具有类型1组中的若干其他类别。
Type 1 | Type 2 | Type 3 | Count |No.Tagged |
____________|_______________|_______________|_______|___________|
Vertebrate | Primate | Gorilla | 2 | 1 |
Vertebrate | Primate | Monkey | 5 | 2 |
Vertebrate | Primate | Ape | 1 | 1 |
Vertebrate | Reptile | Snake | 1 | 1 |
Vertebrate | Reptile | Crocodile | 2 | 1 |
Vertebrate | Rodents | squirrel | 2 | 1 |
Vertebrate | Rodents | Mice | 3 | 3 |
Invertebrate| Arachnids | Scorpion | 2 | 1 |
Invertebrate| Arachnids | Spider | 5 | 3 |
Invertebrate| Crustacean | Crabs | 2 | 2 |
Invertebrate| Crustacean | Shrimp | 2 | 1 |
Invertebrate| Crustacean | Barnacle | 3 | 2 |
Invertebrate| Mollusks | Octopus | 3 | 2 |
Invertebrate| Mollusks | Ammonites | 5 | 2 |
这个数据应该与小计和总数一样:
Type 1 | Type 2 | Type 3 | Count |No. Tagged |
_________________________________________________________________
Vertebrate | Primate | Gorilla | 2 | 1 |
Vertebrate | Primate | Monkey | 5 | 2 |
Vertebrate | Primate | Ape | 1 | 1 |
-----------------------------------------------------------------
Sub Total Primate | 8 | 4 |
-----------------------------------------------------------------
Vertebrate | Reptile | Snake | 1 | 1 |
Vertebrate | Reptile | Crocodile | 2 | 1 |
-----------------------------------------------------------------
Sub Total Reptile | 3 | 2 |
-----------------------------------------------------------------
Vertebrate | Rodents | squirrel | 2 | 1 |
Vertebrate | Rodents | Mice | 3 | 3 |
-----------------------------------------------------------------
Sub Total Rodents | 5 | 4 |
Total Vertebrate| 16 | 10 |
_________________________________________________________________
Invertebrate| Arachnids | Scorpion | 2 | 1 |
Invertebrate| Arachnids | Spider | 5 | 3 |
-----------------------------------------------------------------
Sub Total Arachnids | 7 | 4 |
-----------------------------------------------------------------
Invertebrate| Crustacean | Crabs | 2 | 2 |
Invertebrate| Crustacean | Shrimp | 2 | 1 |
Invertebrate| Crustacean | Barnacle | 3 | 2 |
-----------------------------------------------------------------
Sub Total Crustacean | 7 | 5 |
-----------------------------------------------------------------
Invertebrate| Mollusks | Octopus | 3 | 2 |
Invertebrate| Mollusks | Ammonites | 5 | 2 |
-----------------------------------------------------------------
Sub Total Mollusks | 8 | 4 |
Total Invertebrate | 22 | 13 |
_________________________________________________________________
我曾尝试仅在Excel VBA中以编程方式执行此操作,但我从网上获取此代码并未成功:
Dim Rng As Range
Dim Rw As Long
Dim i As Long
'Sort Data
Rw = Cells(Rows.Count, 3).End(xlUp).Row
Set Rng = Range(Cells(1, 3), Cells(Rw, 3)).Resize(, 4)
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range("C1"), _
SortOn:=xlSortOnValues, Order:=xlAscending
With ActiveSheet.Sort
.SetRange Rng
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Insert spacing
For i = Rw To 3 Step -1
If Cells(i, 3) <> Cells(i - 1, 3) Then
Cells(i, 3).Resize(2).EntireRow.Insert
End If
Next
'Add Grand total
Rw = Cells(Rows.Count, 3).End(xlUp).Row
Cells(Rw + 3, 1) = "Grand Total"
Cells(Rw + 3, 7) = Application.Sum(Range(Cells(Rw, 7), Cells(2, 7)))
'Add Sub Totals
Do
Cells(Rw + 1, 1) = Cells(Rw, 3) & " Total"
If Cells(Rw - 1, 7) = "" Then
'Single items
Cells(Rw + 1, 7) = Cells(Rw, 7)
Rw = Cells(Rw, 3).End(xlUp).Row
Else
'Multiple items
Cells(Rw + 1, 7) = Application.Sum(Range(Cells(Rw, 7), Cells(Rw, 7).End(xlUp)))
Rw = Cells(Rw, 3).End(xlUp).End(xlUp).Row
End If
Loop Until Rw = 1