|
楼主 |
发表于 2021-6-11 09:03
|
显示全部楼层
- Sub 多表拆分() '//可以使用,拆分速度快,2021.6.11
- Dim i%, d, mFile$, arr, arr1, wks As Worksheet, wkb As Workbook
- Dim Start As Double, Finish As Double '运行时间
- Start = Timer
- Application.ScreenUpdating = False
- Set d = CreateObject("scripting.dictionary")
- Dim wb As Workbook
- Set wb = ThisWorkbook
- Dim sht1 As Worksheet, sht2 As Worksheet, sht3 As Worksheet
- Set sht1 = Sheet1 '//考核表
- Set sht2 = Sheet10 '//个人已发
- Set sht3 = Sheet2 '//个人数据
- arr = sht1.[a1].CurrentRegion
- For i = 4 To UBound(arr)
- d(arr(i, 5)) = "" '//这里的5是代表部门所在列
- Next
- arr1 = d.keys
- For i = 0 To UBound(arr1)
- If arr1(i) <> 0 Then '//把值为0的部门排除在外
- Set wkb = Workbooks.Add
- Set wks = wkb.Sheets(1): wks.Name = sht1.Name
- With sht1
- .AutoFilterMode = False
- .Rows("2:2").AutoFilter field:=5, Criteria1:=arr1(i)
- .[a1].CurrentRegion.Offset(1, 0).Copy wkb.Sheets(1).[a1]
- .ShowAllData
- End With
- Set wks = wkb.Sheets.Add(after:=wkb.Sheets(wkb.Sheets.Count))
- wks.Name = sht2.Name
- With sht2
- .AutoFilterMode = False
- .Rows("2:2").AutoFilter field:=5, Criteria1:=arr1(i)
- .[a1].CurrentRegion.Offset(1, 0).Copy wkb.Sheets(2).[a1]
- .ShowAllData
- End With
- Set wks = wkb.Sheets.Add(after:=wkb.Sheets(wkb.Sheets.Count))
- wks.Name = sht3.Name
- With sht3
- .AutoFilterMode = False
- .Rows("4:4").AutoFilter field:=5, Criteria1:=arr1(i)
- .[a4].CurrentRegion.Copy wkb.Sheets(3).[a1]
- .ShowAllData
- End With
- mFile = wb.Path & "" & arr1(i) & ".xlsx"
- If Dir(mFile, vbHidden + vbNormal) <> "" Then
- Kill mFile '//若同名文件存在,则删除
- End If
- wkb.SaveAs Filename:=mFile, FileFormat:=51 '//原格式另存为一个新表
- wkb.Close Savechanges:=False
- End If
- Next
- Application.ScreenUpdating = True
- Finish = Timer
- MsgBox "数据拆分完毕,共用时:" & Finish - Start & "秒"
- End Sub
复制代码
你的代码运行速度挺快的,在我的电脑上大约14秒拆分完毕。别的代码需要2分多钟才拆分好。
我在你的代码的基础上完善了一下,现在运行正常。
代码如下: |
|