OK、これは VBA 内で Excel の SubTotal 関数を使用するスターターです
「ソリューション」が現在データと同じシートにあることを含め、変更したいコードに組み込まれた仮定があります(現在、列Aと行7から始まる「シナリオ」と呼ばれるシートにあります)。これは限られた量のデータで機能しますが、50,000 行に相当します! 必要に応じて統計を要約するコードを追加し、小計を削除できます。元のデータはそのまま残ります。
Sub scenarios()
Dim ws As Worksheet
Dim strow As Long, endrow As Long, stcol As Long, endcol As Long
Dim r As Long, c As Long
Dim newstr As String
Dim cl As Range, rng As Range, drng As Range
Dim strArr() As String
strow = 7
stcol = 1 'Col A
endcol = 7 '7 variables
Set ws = Sheets("Scenarios")
With ws
'find last data row
endrow = Cells(Rows.Count, stcol).End(xlUp).Row
'for each data row
For r = strow To endrow
newstr = ""
'produce concatenated string of that row
For c = stcol To endcol
newstr = newstr & .Cells(r, c)
Next c
'put string into array
ReDim Preserve strArr(r - strow)
strArr(r - strow) = newstr
Next r
'put array to worksheet
Set drng = .Range(.Cells(strow, endcol + 4), .Cells(endrow, endcol + 4))
drng = Application.Transpose(strArr)
'sort newly copied range
drng.Sort Key1:=.Cells(strow, endcol + 4), Order1:=xlAscending, Header:=xlNo
'provide a header row for SubTotal
.Cells(strow - 1, endcol + 4) = "Header"
'resize range to include header
drng.Offset(-1, 0).Resize(drng.Rows.Count + 1, drng.Columns.Count).Select
'apply Excel SubTotal function
Selection.Subtotal GroupBy:=1, Function:=xlCount, Totallist:=Array(1)
End With
End Sub