|
Private Sub 分拆簿_簿c_Click()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim dic As Object, wbk As Workbook, i As Integer
Dim rng As Range '定义一个单元格对象型变量
t = Timer
Set dic = CreateObject("Scripting.Dictionary")
For i = 0 To 列表.ListCount - 1
If 列表.Selected(i) Then
dic(列表.List(i, 0)) = ""
End If
Next i
If dic.Count = 0 Then MsgBox "请至少选择1项": Exit Sub
Set sh = ThisWorkbook.Worksheets("总表")
For Each k In dic.keys
sh.Copy ''拷贝总表为工作簿
Set wbk = ActiveWorkbook ''活动工作表赋值给变量wbk
With wbk.Worksheets(1)
arr = .[a1].CurrentRegion ''
For i = 2 To UBound(arr) '
If VBA.Trim(arr(i, 1)) <> VBA.Trim(k) Then '如果a列不等于关键字
If rng Is Nothing Then '如果rng为空
Set rng = .Rows(i) '整行复制给变量rng
Else
Set rng = Union(rng, .Rows(i)) '用union形成一个聚合,相当于按住ctrl选择不连续区域
End If
End If
Next i
If Not rng Is Nothing Then rng.Delete '
For Each shp In .Shapes
shp.Delete
Next shp '
End With
Set rng = Nothing '初始化变量rng
wbk.SaveAs ThisWorkbook.Path & "\分表\" & k & ".xlsx" '
wbk.Close '
Next k
Application.DisplayAlerts = True
Application.ScreenUpdating = True
Unload UserForm2
MsgBox "耗时:" & Format(Timer - t, "0.000") & "秒"
End Sub
|
|