|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub SplitIntoWorkbooks2()
Dim i As Long, j As Long, var, Fws As Worksheet
Dim dic As Object
With Application
.ScreenUpdating = False '取消屏幕刷新
.DisplayAlerts = False '取消警告信息
.EnableEvents = False '取消事件响应
.EnableCancelKey = xlDisabled '禁用取消键捕获功能
.Calculation = xlCalculationManual '取消公式自动重算
.AskToUpdateLinks = False '取消外链询问
End With
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then strpath = .SelectedItems(1) Else Exit Sub
End With
Call killfile
Set dic = CreateObject("scripting.dictionary")
Set Fws = ActiveSheet
var = Fws.[a1].CurrentRegion
For i = 2 To UBound(var, 1)
If Not dic.exists(var(i, 4) & "_" & var(i, 5) & "_" & var(i, 6)) Then
Set dic(var(i, 4) & "_" & var(i, 5) & "_" & var(i, 6)) = Union(Fws.Cells(i, 1), Fws.[a1])
Else
Set dic(var(i, 4) & "_" & var(i, 5) & "_" & var(i, 6)) = Union(Fws.Cells(i, 1), dic(var(i, 4) & "_" & var(i, 5) & "_" & var(i, 6)))
End If
Next i
For i = 1 To dic.Count
Fws.Copy
ActiveSheet.Cells.Clear
dic.items()(i - 1).EntireRow.Copy ActiveSheet.[a1]
With ActiveWorkbook
.ActiveSheet.Name = dic.keys()(i - 1)
.ActiveSheet.Rows.AutoFit
.ActiveSheet.Columns.AutoFit
.SaveAs strpath & "\" & dic.keys()(i - 1) & ".xlsx"
.Close True
End With
Next i
With Application
.ScreenUpdating = True '恢复屏幕刷新
.DisplayAlerts = True '恢复警告信息
.EnableEvents = True '恢复事件响应
.EnableCancelKey = xlInterrupt '恢复取消键捕获功能
.Calculation = xlCalculationAutomatic '恢复公式自动重算
.AskToUpdateLinks = True '恢复外链询问
End With
End Sub
Sub killfile()
Dim fso As Object, file As Object
Set fso = CreateObject("scripting.FileSystemObject")
On Error Resume Next
For Each file In fso.GetFolder(strpath).Files
file.Delete (True)
Next file
End Sub
|
|