vba
1Sub Test1()
2
3 Dim wsSource As Worksheet
4 Dim rngTarget As Range
5 Dim lngRow As Long
6 Dim lngFirstRow As Long
7 Dim lngLastRow As Long
8 Dim lngSequenceStart As Long
9 Dim lngSequenceHeight As Long
10
11 Set wsSource = ThisWorkbook.Worksheets(1)
12
13 With wsSource
14
15 lngFirstRow = 2
16 lngLastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
17
18 If lngFirstRow > lngLastRow Then
19 Set wsSource = Nothing
20 Exit Sub
21 End If
22
23 .Range(.Cells(lngFirstRow, 3), _
24 .Cells(lngLastRow, 4)).ClearContents
25
26 lngSequenceStart = lngFirstRow
27 lngSequenceHeight = 1
28
29 For lngRow = lngFirstRow To lngLastRow
30 If .Cells(lngRow, 1).Value < .Cells(lngRow + 1, 1).Value Then
31 lngSequenceHeight = lngSequenceHeight + 1
32 Else
33 '集計範囲の参照
34 Set rngTarget = .Cells(lngSequenceStart, 2).Resize(lngSequenceHeight, 1)
35 '中央値の出力
36 .Cells(lngRow, 3).Value = WorksheetFunction.Median(rngTarget)
37 '検算用に数式も設定
38 .Cells(lngRow, 4).Formula = "=MEDIAN(" & rngTarget.Address & ")"
39 Set rngTarget = Nothing
40 lngSequenceStart = lngRow + 1
41 lngSequenceHeight = 1
42 End If
43 Next
44
45 End With
46
47 Set wsSource = Nothing
以上のようなコードを実行できればよい、ということでしょうか。