|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
tplk 发表于 2013-2-14 21:44
老师,想增加一个新功能,请看看,感谢啦 - Dim dicValidation As Object
- Dim dicTeacher As Object
- Dim arrTeacher As Variant
- Private Sub Worksheet_Activate()
- Call Macro1
- End Sub
- Sub Macro1()
- Dim arr, i&, s$
- Set dicValidation = CreateObject("scripting.dictionary")
- Set dicTeacher = CreateObject("scripting.dictionary")
- With Sheets("学生成绩源")
- arr = .Range("A2:C" & .Range("A65536").End(xlUp).Row)
- End With
- 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
- arrTeacher = Sheets("老师课表").[a1].CurrentRegion
- For i = 2 To UBound(arrTeacher)
- dicTeacher(arrTeacher(i, 1) & arrTeacher(i, 2)) = i
- Next
- 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 dicValidation Is Nothing Then Call Macro1
- 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
- If [b3] = "" Then Exit Sub
- 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(班, rng.Cells(rng.Rows.Count), , 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
- i = dicTeacher([g1] & [b3])
- [d3] = "任课老师 " & arrTeacher(1, 3) & ":" & arrTeacher(i, 3) & " " & arrTeacher(1, 4) & ":" & arrTeacher(i, 4) & " " & arrTeacher(1, 5) & ":" & arrTeacher(i, 5)
- End Sub
复制代码 |
|