|
Public Sub 选择科目()
Application.ScreenUpdating = False
Set stp1 = Worksheets("数据源") 'stp1指向学生数据sheet
Set stp2 = Worksheets("统计表") 'stp2指向监考sheet
Set Rng = stp2.Cells(1).CurrentRegion
Set dic = CreateObject("scripting.dictionary") '建立关键字的字典
For i = 2 To UBound(arr)
If Not dic.exists(arr(i, 1) & "") Then
ReDim brr(1 To arr(i, 5), 1 To 4)
For j = 1 To arr(i, 5)
brr(j, 1) = arr(i, 1): brr(j, 2) = arr(i, 3): brr(j, 3) = arr(i, 4)
Next
Set wb = Application.Workbooks.Add '指向新建工作簿
With wb
.SaveAs Filename:=ThisWorkbook.Path & "\" & Replace(arr(i, 1), "/", "-") & ".xlsx", ReadOnlyRecommended:=1
Set shtp = 复制工作表(stp2, Replace(arr(i, 1), "/", "-"), wb)
shtp.Cells(5, 1).Resize(arr(i, 5), 4) = brr
.Close False
End With
End If
Next
End Sub
Public Function 复制工作表(stp As Worksheet, st$, Optional wb = Nothing) '将stp工作表复制到wb工作薄并重命名为st
Application.DisplayAlerts = False '关闭警告提示
If wb Is Nothing Then Set wb = stp.Parent
With wb
stp.Copy After:=Worksheets(Worksheets.Count) '复制工作表
On Error Resume Next '忽略错误继续执行VBA代码,避免出现错误消息
mingming2:
Err.Clear
ActiveSheet.Name = st '重命名
If Err.Nunber = 1004 Then '如果重命名失败,即说明工作表存在,则删除
.Worksheets(st).Delete '删除st工作表
GoTo mingming2
End If
On Error GoTo 0 '以下恢复捕捉代码出现错误消息
'.Worksheets(st).Tab.ColorIndex = 3 '工作表标签红色
Set 复制工作表 = .Worksheets(st) '返回新建工作表
End With
Application.DisplayAlerts = True '打开警告提示
End Function |
|