|
下面这段程序的目的是把学生总表中的学生分别按班级保存不同的工作簿,但是保存工作簿时达不到目标,请大侠修改一下:
Application.ScreenUpdating = False '关闭屏幕更新
Dim Arr
Dim Nrr() As String
Dim i, j, k, n As Integer
Dim folder As String
folder = ThisWorkbook.Path & "\班级成绩表"
If Len(Dir(folder, vbDirectory)) = 0 Then MkDir folder '如果文件夹不存在,新建文件夹
'下面这段程序的功能是找到总表第7列中所有的班级数
Arr = [A1].CurrentRegion '总表内容存入2维数组Arr
i = 1
j = 2
n = Range("A1").CurrentRegion.Rows.Count '总表的数据总行数
ReDim Nrr(1 To n)
Nrr(i) = Arr(j, 7) '7列为班级名称,名称将作为保存工作簿的名称,用1维数组Nrr存放
For j = 3 To n
If Arr(j, 7) <> Nrr(i) Then
i = i + 1
Nrr(i) = Arr(j, 7)
End If
Next
'如果筛选,显示所有的数据
With Worksheets("Sheet1")
If .AutoFilterMode Then
If .FilterMode = True Then .ShowAllData
End If
End With
For k = 1 To i
Workbooks.Add
ActiveWorkbook.SaveAs Filename:="C:\VBATEST\book.xlsx"
With Workbooks("总表.xlsx").Worksheets("Sheet1")
.Range("A1").AutoFilter _
Field:=7, Criteria1:=Nrr(k)
.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Copy _
Workbooks("book").Worksheets(1).[A1]
End With
Next
Application.ScreenUpdating = True '开启屏幕更新
|
|