tplk 发表于 2013-2-14 11:07
老师,新年好,这是一个双条件查询的求助,请进
太久了,已经全忘了,先请测试:- Dim dicValidation As Object
- Private Sub Worksheet_SelectionChange(ByVal Target As Range)
- If Target.Address(0, 0) <> "G1" And Target.Address(0, 0) <> "B3" Then Exit Sub
- Dim arr, i&, s$
- With Sheets("学生成绩源")
- arr = .Range("A2:C" & .Range("A65536").End(xlUp).Row)
- End With
- If dicValidation Is Nothing Then Set dicValidation = CreateObject("scripting.dictionary")
- For i = 1 To UBound(arr)
- If Not dicValidation.Exists(arr(i, 1)) Then Set dicValidation(arr(i, 1)) = CreateObject("scripting.dictionary")
- dicValidation(arr(i, 1))(arr(i, 3)) = ""
- Next
- With [g1].Validation
- .Delete
- .Add 3, 1, 1, Join(dicValidation.keys, ",")
- End With
- End Sub
- Private Sub Worksheet_Change(ByVal Target As Range)
- If Target.Address(0, 0) <> "B3" And Target.Address(0, 0) <> "G1" Then Exit Sub
- If [g1] = "" Or [b3] = "" Then Exit Sub
- If Target.Address(0, 0) = "G1" Then
- With [b3].Validation
- .Delete
- If dicValidation.Exists(Target.Value) Then .Add 3, 1, 1, Join(dicValidation(Target.Value).keys, ",")
- End With
- [b3] = ""
- Exit Sub
- End If
- Range("A5:O38").ClearContents
- Dim c As Range, c2 As Range, arr, i&, 校$, 班$, brr(100, 1 To 2), n&, rng As Range
- Dim crr(1 To 34, 1 To 15), hj As Double, j&, m&, mc&
- 校 = Range("g1").Value
- 班 = Range("b3").Value
- With Sheets("学生成绩源")
- Set c = .[a:a].Find(校, , , xlWhole)
- If c Is Nothing Then
- MsgBox 校 & "没有查到"
- Exit Sub
- End If
- Set rng = c.Offset(, 2).Resize(.[a:a].Find(校, , , xlWhole, , xlPrevious).Row - c.Row + 1)
- Set c2 = rng.Find(班, , , xlWhole)
- If c2 Is Nothing Then
- MsgBox 班 & "没有查到"
- Exit Sub
- End If
- arr = c2.Resize(rng.Find(班, , , xlWhole, , xlPrevious).Row - c2.Row + 1, 5)
- End With
- n = 1
- brr(1, 2) = 0
- For i = 1 To UBound(arr)
- hj = arr(i, 3) + arr(i, 4) + arr(i, 5)
- For j = 1 To n
- If hj >= brr(j, 2) Then
- For m = n To j Step -1
- brr(m + 1, 1) = brr(m, 1)
- brr(m + 1, 2) = brr(m, 2)
- Next
- brr(j, 1) = i
- brr(j, 2) = hj
- Exit For
- End If
- Next
- n = n + 1
- Next
- For i = 1 To n - 1
- If i > 34 Then
- x = i - 34
- y = 8
- Else
- x = i
- y = 0
- End If
- If brr(i, 2) <> brr(i - 1, 2) Then mc = i
- crr(x, 1 + y) = mc
- For j = 2 To 5
- crr(x, j + y) = arr(brr(i, 1), j)
- Next
- crr(x, 6 + y) = brr(i, 2)
- Next
- Range("a5:o38") = crr
- End Sub
复制代码 |