ほとんどの場合、数式は VBA よりも優れた選択肢であるというスコットの意見に同意します。ただし、データの並べ替え、結果を別のタブに表示するなど、いくつかの特定のことを要求したため、この場合、マクロに伴う余分なオーバーヘッドで問題ないと思います。
データに関するいくつかの詳細 (データ型、特定のセルの場所など) を省略しました... コード内のコメントを参照してください。カスタマイズが必要になる場合があります。
私はあなたの質問をできるだけ文字通りに解釈しようとしました... たとえば、個人的には、表を横切るよりも下に行く結果を好むかもしれませんが、それは私が答えにアプローチした方法ではありません.
Sub SummarizeDecade()
Const YearColumn As Integer = 2 'assumes the first year is in cell B2
Const FirstDataRow As Integer = 2 'assumes the first year is in cell B2
Dim wb As Excel.Workbook, ws As Excel.Worksheet, wsNew As Excel.Worksheet
Dim rowStart As Long, rowEnd As Long, colPaste As Long
Dim decade As Integer
Dim avg As Double, mini As Double, maxi As Double 'you didn't specify data type, Double is most accommodating
Set wb = ThisWorkbook
Set ws = wb.ActiveSheet 'assumes you run the macro while the data sheet is the current sheet; would prefer to use a sheet name
'setup new worksheet for summary results, as requested
wb.Worksheets.Add
Set wsNew = wb.Worksheets(1)
wsNew.Name = "Results"
wsNew.Cells(1, 1).Value = "Decade"
wsNew.Cells(2, 1).Value = "Average"
wsNew.Cells(3, 1).Value = "Minimum"
wsNew.Cells(4, 1).Value = "Maximum"
colPaste = 2
ws.Activate
ws.Cells(FirstDataRow, YearColumn).Sort ws.Cells(FirstDataRow, YearColumn), xlAscending, , , , , , xlYes 'sorts the data by year, as requested
rowStart = FirstDataRow
rowEnd = rowStart
Do Until Len(ws.Cells(rowEnd, 3).Value) = 0 'be sure your data does not include strings with spaces, zeroes, etc. Must be blank/null/empty.
decade = Int(ws.Cells(rowStart, YearColumn) / 10) * 10
Do Until Int(ws.Cells(rowEnd, YearColumn) / 10) * 10 <> decade
rowEnd = rowEnd + 1
Loop
'calculate the average, max, and min
avg = Application.WorksheetFunction.Average(ws.Range(ws.Cells(rowStart, 3), ws.Cells(rowEnd - 1, 3)))
mini = Application.WorksheetFunction.min(ws.Range(ws.Cells(rowStart, 3), ws.Cells(rowEnd - 1, 3)))
maxi = Application.WorksheetFunction.max(ws.Range(ws.Cells(rowStart, 3), ws.Cells(rowEnd - 1, 3)))
'write the summaries on the new worksheet tab
wsNew.Cells(1, colPaste).Value = decade
wsNew.Cells(2, colPaste).Value = avg
wsNew.Cells(3, colPaste).Value = mini
wsNew.Cells(4, colPaste).Value = maxi
colPaste = colPaste + 1
rowStart = rowEnd
Loop
End Sub