これを行うには、組み込みの Excel 機能を使用することをお勧めします。ループする値が多数ある場合、ループには時間がかかり、問題が発生する可能性があります。
次のようなもので、ピボット テーブルの作成を開始できます ( http://www.ozgrid.com/News/pivot-tables.htmから) 。
Sub MakeTable()
Dim Pt As PivotTable
Dim strField As String
'Pass heading to a String variable
strField = Selection.Cells(1, 1).Text
'Name the list range
Range(Selection, Selection.End(xlDown)).Name = "Items"
'Create the Pivot Table based off our named list range.
'TableDestination:="" will force it onto a new sheet
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, _
SourceData:="=Items").CreatePivotTable TableDestination:="", _
TableName:="ItemList"
'Set a Pivot Table variable to our new Pivot Table
Set Pt = ActiveSheet.PivotTables("ItemList")
'Place the Pivot Table to Start from A3 on the new sheet
ActiveSheet.PivotTableWizard TableDestination:=Cells(3, 1)
'Move the list heading to the Row Field
Pt.AddFields RowFields:=strField
'Move the list heading to the Data Field
Pt.PivotFields(strField).Orientation = xlDataField
End Sub
これは小計用です (ただし、私はピボット テーブルを好みます)
http://msdn.microsoft.com/en-us/library/aa213577(office.11 ).aspx
編集:私は再読して、次の考えを持っています。別のシートにピボット テーブルを設定し (コードを使用せずに)、シートが選択されるたびにテーブルが更新されるように、ピボット テーブルのあるシートに次のコードを配置します。
Private Sub Worksheet_Activate()
Dim pt As PivotTable
'change "MiPivot" to the name of your pivot table
Set pt = ActiveSheet.PivotTables("MyPivot")
pt.RefreshTable
End Sub
編集 #2 シートのすべてのピボット テーブルを更新
http://www.ozgrid.com/VBA/pivot-table-refresh.htm
Private Sub Worksheet_Activate()
Dim pt As PivotTable
For Each pt In ActiveSheet.PivotTables
pt.RefreshTable
Next pt
End Sub
リンクには、シート/ワークブックのピボット テーブルを更新する方法に関する複数のオプションがあります。