- Sub 中位数()
- Dim sht As Worksheet, i, n, m, x As Integer, arr(), arryj(), arrzc()
- '重新定义数组
- ReDim arr(1 To 2, 1 To 2) '基层管理人员
- ReDim arryj(1 To 2, 1 To 2) '基层管理人员一级
- ReDim arrzc(1 To 2, 1 To 2) '中层管理人员正职
- '设定sht为工作簿的第一个表
- Set sht = ThisWorkbook.Worksheets(1)
- '循环类别列
- For i = 3 To sht.Cells(Rows.Count, 2).End(xlUp).Row
- '设定条件,将符合条件的列入数组
- Select Case sht.Cells(i, 2).Value
- Case Is = "基层管理人员"
- '再定义数组
- n = n + 1
- ReDim Preserve arr(1 To 2, 1 To n)
- arr(1, n) = sht.Cells(i, 3).Value '写入年龄值
- arr(2, n) = sht.Cells(i, 4).Value '写入工龄值
- Case Is = "基层管理人员一级"
- m = m + 1
- ReDim Preserve arryj(1 To 2, 1 To m)
- arryj(1, m) = sht.Cells(i, 3).Value '写入年龄值
- arryj(2, m) = sht.Cells(i, 4).Value '写入工龄值
- Case Is = "中层管理人员正职"
- x = x + 1
- ReDim Preserve arrzc(1 To 2, 1 To x)
- arrzc(1, x) = sht.Cells(i, 3).Value '写入年龄值
- arrzc(2, x) = sht.Cells(i, 4).Value '写入工龄值
- End Select
- Next
- '将中位数写入计算输出表
- With WorksheetFunction
- '写入基层管理人员的年龄与工龄中位数
- sht.Cells(3, 7).Value = Application.Median(.Index(arr, 1, 0))
- sht.Cells(3, 8).Value = Application.Median(.Index(arr, 2, 0))
- '写入基层管理人员一级的年龄与工龄中位数
- sht.Cells(4, 7).Value = Application.Median(.Index(arryj, 1, 0))
- sht.Cells(4, 8).Value = Application.Median(.Index(arryj, 2, 0))
- '写入中层管理人员正职的年龄与工龄中位数
- sht.Cells(5, 7).Value = Application.Median(.Index(arrzc, 1, 0))
- sht.Cells(5, 8).Value = Application.Median(.Index(arrzc, 2, 0))
- End With
- End Sub
复制代码 |