|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
'项目3列变1列了,如果是这样直接使用工作表的筛选功能就可以了
Option Explicit
Dim data
Private Sub CommandButton1_Click()
Dim i, j, k, n, m, cnt, datatemp, sht
ReDim arr(1 To UBound(data, 1), 5)
cnt = UBound(data, 1): datatemp = data
If CheckBox1.Value Then
For i = 2 To UBound(datatemp, 1)
If datatemp(i, 5) = ComboBox1.Text Then
n = n + 1
For j = 1 To 5: datatemp(n, j) = datatemp(i, j): Next
End If
Next
cnt = n
End If
n = 0
If CheckBox2.Value Then
For i = 1 To cnt
If datatemp(i, 4) = ComboBox2.Text Then
n = n + 1
For j = 1 To 5: datatemp(n, j) = datatemp(i, j): Next
End If
Next
cnt = n
End If
n = 0
If CheckBox3.Value Then
For i = 1 To cnt
If datatemp(i, 3) = ComboBox3.Text Then
n = n + 1
For j = 1 To 5: datatemp(n, j) = datatemp(i, j): Next
End If
Next
cnt = n
End If
If CheckBox1.Value Or CheckBox2.Value Or CheckBox3.Value Then
If cnt > 0 Then
For i = 1 To cnt
For j = 1 To 5
arr(i, j) = datatemp(i, j)
Next j, i
Call bsort(arr, 1, cnt)
For i = 1 To cnt: arr(i, 0) = i: Next
sht = IIf(CheckBox1.Value, ComboBox1.Text, vbNullString) & _
IIf(CheckBox2.Value, ComboBox2.Text, vbNullString) & _
IIf(CheckBox3.Value, ComboBox3.Text, vbNullString) & "组"
n = 0
For Each i In Sheets
If i.Name = sht Then n = 1: Exit For
Next
If n = 0 Then Sheets.Add: ActiveSheet.Name = sht
End If
Else
cnt = 0
End If
If Len(sht) > 0 Then
With Sheets(sht).[a2]
.Resize(Rows.Count - 1, UBound(arr, 2) + 1).ClearContents
If cnt > 0 Then .Resize(cnt, UBound(arr, 2) + 1) = arr
.Offset(-1).Resize(, 6) = Split("序号 学校名称 姓名 性别 类别 项目")
End With
End If
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Private Sub UserForm_Initialize()
Dim i, j, dic(2)
For i = 0 To UBound(dic)
Set dic(i) = CreateObject("scripting.dictionary")
Next
data = Sheets("sheet1").UsedRange
For i = 2 To UBound(data, 1)
dic(1)(data(i, 4)) = vbNullString
dic(2)(data(i, 3)) = vbNullString
dic(0)(data(i, 5)) = vbNullString
Next
For Each i In dic(0).keys: ComboBox1.AddItem i: Next: ComboBox1.ListIndex = 0
For Each i In dic(1).keys: ComboBox2.AddItem i: Next: ComboBox2.ListIndex = 0
For Each i In dic(2).keys: ComboBox3.AddItem i: Next: ComboBox3.ListIndex = 0
CheckBox1.Value = 1: CheckBox2.Value = 1: CheckBox3.Value = 1
End Sub
Function bsort(arr, first, last)
Dim i, j, k, kk, t, move As Boolean
For i = first To last - 1
For j = first To last + first - 1 - i
For k = LBound(arr, 2) To UBound(arr, 2) - 1
If arr(j, k) > arr(j + 1, k) Then
For kk = k To UBound(arr, 2)
t = arr(j, kk): arr(j, kk) = arr(j + 1, kk): arr(j + 1, kk) = t
Next
move = True: Exit For
ElseIf arr(j, k) = arr(j + 1, k) Then
If arr(j, k + 1) > arr(j + 1, k + 1) Then
For kk = k + 1 To UBound(arr, 2)
t = arr(j, kk): arr(j, kk) = arr(j + 1, kk): arr(j + 1, kk) = t
Next
move = True
ElseIf arr(j, k + 1) < arr(j + 1, k + 1) Then
Exit For
End If
Else
Exit For
End If
Next k, j
If Not move Then Exit For
Next
End Function |
|