|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 拆分()
Application.ScreenUpdating = False
Dim ar As Variant, br As Variant
Dim d As Object, dc As Object
Dim arr()
Set d = CreateObject("scripting.dictionary")
Set dc = CreateObject("scripting.dictionary")
Application.DisplayAlerts = False
For Each sh In Sheets
If sh.Index > 2 Then
sh.Delete
End If
Next sh
Application.DisplayAlerts = True
Set sht = Sheets("总表")
With Sheets("班主任")
r = .Cells(Rows.Count, 1).End(xlUp).Row
If r < 2 Then MsgBox "班主任工作表为空!": End
ar = .Range("a1:b" & r)
End With
For i = 2 To UBound(ar)
d(ar(i, 1)) = ar(i, 2)
Next i
With sht
rs = .Cells(Rows.Count, 1).End(xlUp).Row
y = .Cells(2, Columns.Count).End(xlToLeft).Column
If rs < 3 Then MsgBox "总表为空!": End
br = .Range(.Cells(2, 1), .Cells(rs, y))
End With
For i = 2 To UBound(br)
If Trim(br(i, 3)) <> "" Then
dc(br(i, 3)) = ""
End If
Next i
For Each k In dc.keys
n = 0
ReDim arr(1 To UBound(br), 1 To UBound(br, 2) - 1)
For i = 2 To UBound(br)
If br(i, 3) = k Then
n = n + 1
arr(n, 1) = n
arr(n, 2) = br(i, 2)
For j = 4 To UBound(br, 2)
arr(n, j - 1) = br(i, j)
Next j
End If
Next i
sht.Copy after:=Sheets(Sheets.Count)
With ActiveSheet
.Name = k
.Columns(3).Delete
.Rows("2:2").Insert Shift:=xlDown
.Range(.Cells(1, 1), .Cells(1, y)).Borders.LineStyle = 0
With .Range(.Cells(2, 1), .Cells(2, y)).Font
.Name = "宋体"
.Size = 12
.Bold = True
End With
With .Range(.Cells(4, 1), .Cells(r, y))
.ClearContents
.Borders.LineStyle = 0
End With
.[a2] = "班级:"
.[h2] = "班主任:"
.[b2] = k
.[i2] = d(k)
.[a4].Resize(n, UBound(arr, 2)) = arr
.[a4].Resize(n, UBound(arr, 2)).Borders.LineStyle = 1
End With
Next k
Application.ScreenUpdating = True
MsgBox "拆分完毕!"
End Sub
|
|