|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub chaifenshuju()
'按要求列拆分数据
Dim sht, sht0 As Worksheet
Dim k, i, j As Integer
Dim irow As Integer '这个说的是一共多少行
Dim L As Integer
Set sht0 = ActiveSheet
L = InputBox("请输入你要按哪列分")
'删除无意义的表
Application.DisplayAlerts = False
If Sheets.Count > 1 Then
For Each sht In Sheets
If sht.name <> sht0.name Then
sht.Delete
End If
Next
End If
Application.DisplayAlerts = True
irow = sht0.Range("a65536").End(xlUp).Row
'拆分表
For i = 2 To irow
k = 0
For Each sht In Sheets
If sht.name = sht0.Cells(i, L) Then
k = 1
End If
Next
If k = 0 Then
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).name = sht0.Cells(i, L)
End If
Next
'拷贝数据
For j = 2 To Sheets.Count
sht0.Range("a1:z" & irow).AutoFilter Field:=L, Criteria1:=Sheets(j).name
sht0.Range("a1:z" & irow).Copy Sheets(j).Range("a1")
Next
sht0.Range("a1:z" & irow).AutoFilter
sht0.Select
MsgBox "已处理完毕"
End Sub |
|