|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 拆分()
Application.ScreenUpdating = False
Dim ar As Variant
Dim i As Long, r As Long, rs As Long
Dim br()
Dim d As Object
Set d = CreateObject("scripting.dictionary")
For Each sh In Sheets(Array("Summary", "Contract", "Revnue"))
If sh.Name = "Summary" Then
ks = 4
js = 17
lh = 1
ElseIf sh.Name = "Contract" Then
ks = 3
js = 9
lh = 6
ElseIf sh.Name = "Revnue" Then
ks = 3
js = 10
lh = 8
End If
r = sh.Cells(sh.Rows.Count, lh).End(xlUp).Row
ar = sh.Range(sh.Cells(1, 1), sh.Cells(r, js))
For i = ks To UBound(ar)
If Trim(ar(i, lh)) <> "" Then
d(Trim(ar(i, lh))) = ""
End If
Next i
Next sh
For Each k In d.keys
m = 0
For Each sh In Sheets(Array("Summary", "Contract", "Revnue"))
m = m + 1
If sh.Name = "Summary" Then
ks = 3
js = 17
lh = 1
ElseIf sh.Name = "Contract" Then
ks = 2
js = 9
lh = 6
ElseIf sh.Name = "Revnue" Then
ks = 2
js = 10
lh = 8
End If
r = sh.Cells(sh.Rows.Count, lh).End(xlUp).Row
ar = sh.Range(sh.Cells(1, 1), sh.Cells(r, js))
n = 0
ReDim br(1 To UBound(ar), 1 To js)
For i = ks To UBound(ar)
If Trim(ar(i, lh)) = k Then
n = n + 1
For j = 1 To UBound(ar, 2)
br(n, j) = ar(i, j)
Next j
End If
Next i
If m = 1 Then
sh.Copy
Set wb = ActiveWorkbook
Else
sh.Copy after:=wb.Worksheets(wb.Worksheets.Count)
End If
With wb.Worksheets(sh.Name)
.Range(.Cells(ks, 1), .Cells(r, js)).Borders.LineStyle = 0
.Range(.Cells(ks, 1), .Cells(r, js)) = Empty
If n > 0 Then
.Cells(ks, 1).Resize(n, UBound(br, 2)) = br
.Cells(ks, 1).Resize(n, UBound(br, 2)).Borders.LineStyle = 1
End If
End With
Next sh
wb.SaveAs Filename:=ThisWorkbook.Path & "\拆分文件\" & k & ".xlsx"
wb.Close
Next k
Application.ScreenUpdating = True
MsgBox "ok!"
End Sub
|
|