|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
老师,我还想请教一下,一下代码要怎么改才可支持拆分之后的文件是xls的格式?
Sub chaifen()
Set d = CreateObject("scripting.dictionary")
Dim rg As Range
On Error Resume Next
Set rg = Application.InputBox("请框选拆分依据列!只能选择单列单元格区域!", Title:="提示", Type:=8)
If rg Is Nothing Then MsgBox "您没有选择拆分依据列": Exit Sub
Application.ScreenUpdating = False '关闭屏幕更新
r = rg.Column
p = InputBox("请输入标题行数", "标题行", "1")
If p = "" Then MsgBox "您没有输入标题行数": Exit Sub
pp = MsgBox("拆分为工作表选【是】,拆分为工作簿选【否】", vbYesNo)
Set sh = ThisWorkbook.ActiveSheet
ar = sh.[a1].CurrentRegion
For i = Val(p) + 1 To UBound(ar)
If Trim(ar(i, r)) <> "" Then
d(Trim(ar(i, r))) = ""
End If
Next i
If pp = vbNo Then GoTo 10
Application.DisplayAlerts = False '关闭警告信息提示
For Each sht In Worksheets '遍历一遍工作表,如果字典中存在则删除
If d.exists(sht.Name) Then sht.Delete
Next sht
Application.DisplayAlerts = True
10:
Dim rng As Range
For Each k In d.keys
If pp = vbYes Then
sh.Copy after:=Sheets(Sheets.Count)
With Sheets(Sheets.Count)
For i = Val(p) + 1 To UBound(ar)
If Trim(.Cells(i, r)) <> k Then
If rng Is Nothing Then
Set rng = .Rows(i)
Else
Set rng = Union(rng, .Rows(i))
End If
End If
Next i
rng.Delete
For Each ss In .Shapes
ss.Delete
Next ss
.Name = k
End With
Set rng = Nothing
ElseIf pp = vbNo Then
sh.Copy
With ActiveWorkbook.Worksheets(1)
For i = Val(p) + 1 To UBound(ar)
If Trim(.Cells(i, r)) <> k Then
If rng Is Nothing Then
Set rng = .Rows(i)
Else
Set rng = Union(rng, .Rows(i))
End If
End If
Next i
rng.Delete
For Each ss In .Shapes
ss.Delete
Next ss
.Name = k
End With
Set rng = Nothing
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & k
ActiveWorkbook.Close
End If
Next k
Set d = Nothing
Application.ScreenUpdating = True '恢复屏幕更新
MsgBox "数据拆分完成!"
End Sub
|
|