搜档网
当前位置:搜档网 › excel拆分表操作

excel拆分表操作

Sub 拆分表()
Dim arr, rng As Range, d As Object, k, t, sh As Worksheet, i&
Set rng = Range("A1:F1")
arr = Range("A1:A" & Range("A65536").End(xlUp).Row)
Set d = CreateObject("scripting.dictionary")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each sh In Sheets
If https://www.sodocs.net/doc/9a10432314.html, <> "总表" Then sh.Delete
Next
Application.DisplayAlerts = True
For i = 2 To UBound(arr)
If Not d.Exists(arr(i, 1)) Then
Set d(arr(i, 1)) = Cells(i, 1).Resize(1, 6)
Else
Set d(arr(i, 1)) = Union(d(arr(i, 1)), Cells(i, 1).Resize(1, 6))
End If
Next
k = d.Keys
t = d.Items
With Sheets
For i = 0 To d.Count - 1
With .Add(After:=.Item(.Count))
.Name = k(i)
rng.Copy .Range("A1")
t(i).Copy .Range("A2")
End With
Next
End With
Sheets(1).Activate
Application.ScreenUpdating = True
MsgBox "完毕"
End Sub



Sub 拆分表_新文件()
Dim arr As Variant
Dim i As Long
Dim lc As Integer
Dim p As String
Dim bLowVersion As Boolean
Dim sh As Worksheet
Dim rng As Range
Application.ScreenUpdating = False
Application.DisplayAlerts = False
arr = Range("A1").CurrentRegion
lc = UBound(arr, 2)
Set sh = ActiveSheet
Set rng = Rows(1)
p = ThisWorkbook.Path & "\"
If Application.Version <= 11 Then bLowVersion = True
For i = 2 To UBound(arr)
Set wb = Workbooks.Add(xlWBATWorksheet)
With wb.Sheets(1)
rng.Copy .[a1]
sh.Cells(i, 1).Resize(, lc).Copy .[a2]
End With
If bLowVersion Then
wb.SaveAs Filename:=p & arr(i, 1) & ".xls", FileFormat:=-4143, CreateBackup:=False
Else
wb.SaveAs Filename:=p & arr(i, 1) & ".xlsx", FileFormat:=51, CreateBackup:=False
End If
wb.Close
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "完毕。", vbInformation
End Sub



Sub 按条件创表()
Dim arr, rng As Range, d As Object, k, t, sh As Worksheet, i&
Dim rng_new As Range
Set rng = Range("A1:F1")
arr = Range("A1:A" & Range("A65536").End(xlUp).Row)
Set d = CreateObject("scripting.dictionary")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each sh In Sheets
If https://www.sodocs.net/doc/9a10432314.html, <> "总表" Then sh.Delete
Next
Application.DisplayAlerts = True
For i = 2 To UBound(arr)
If Not d.Exists(arr(i, 1)) Then
Set d(arr(i, 1)) = Cells(i, 1).Resize(1, 6)
Else
Set d(arr(i, 1)) = Union(d(arr(i, 1)), Cells(i, 1).Resize(1, 6))
End If
Next
k = d.Keys
t = d.Items

Set rng_new = Rows(1)
p = ThisWorkbook.Path & "\"
If Application

.Version <= 11 Then bLowVersion = True

For i = 0 To d.Count - 1
Set wb = Workbooks.Add(xlWBATWorksheet)
With wb.Sheets(1)
rng_new.Copy .Range("A1")
t(i).Copy .Range("A2")
End With
If bLowVersion Then
wb.SaveAs Filename:=p & k(i) & ".xls", FileFormat:=-4143, CreateBackup:=False
Else
wb.SaveAs Filename:=p & k(i) & ".xlsx", FileFormat:=51, CreateBackup:=False
End If
wb.Close
Next

Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "完毕。", vbInformation
End Sub


Sub 按条件创表2()
Dim arr, rng As Range, d As Object, k, t, sh As Worksheet, i&
Dim rng_new As Range
Set rng = Range("A1:F1")
arr = Range("A1:A" & Range("A65536").End(xlUp).Row)
Set d = CreateObject("scripting.dictionary")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each sh In Sheets
If https://www.sodocs.net/doc/9a10432314.html, <> "总表" Then sh.Delete
Next
Application.DisplayAlerts = True
For i = 2 To UBound(arr)
If Not d.Exists(arr(i, 1)) Then
Set d(arr(i, 1)) = Cells(i, 1).Resize(1, 6)
Else
Set d(arr(i, 1)) = Union(d(arr(i, 1)), Cells(i, 1).Resize(1, 6))
End If
Next
k = d.Keys
t = d.Items

Set rng_new = Rows(1)
p = ThisWorkbook.Path & "\"
If Application.Version <= 11 Then bLowVersion = True

For i = 0 To d.Count - 1
Set wb = Workbooks.Add(xlWBATWorksheet)
With wb.Sheets(1)
rng_new.Copy .Range("A1")
t(i).Copy .Range("A2")
End With

sPath = p & k(i)
If Len(Dir(sPath, vbDirectory)) = 0 Then
MkDir sPath
End If


If bLowVersion Then
wb.SaveAs Filename:=sPath & "\" & "bb.xls", FileFormat:=-4143, CreateBackup:=False
Else
wb.SaveAs Filename:=sPath & "\" & "bb.xlsx", FileFormat:=51, CreateBackup:=False
End If
wb.Close
Next

Application.DisplayAlerts = True
Application.ScreenUpdating = True
MsgBox "完毕。", vbInformation
End Sub

相关主题