|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 拆分()
Application.ScreenUpdating = False
Dim ar As Variant
Dim sh As Worksheet
With Sheets("数据源")
r = .Cells(Rows.Count, 1).End(xlUp).Row
ar = .Range("a1:d" & r)
rs = .Cells(Rows.Count, 7).End(xlUp).Row
br = .Range("g1:i" & rs)
End With
w = MsgBox("拆分为工作表选择是,拆分为工作簿选择否", vbYesNo)
If w = "" Then End
Application.DisplayAlerts = False
If w = vbYes Then
For Each sht In Sheets
If sht.Index > 2 Then
sht.Delete
End If
Next sht
End If
Application.DisplayAlerts = True
Set sh = ThisWorkbook.Worksheets("模板")
For i = 2 To UBound(br)
n = 0
ReDim arr(1 To UBound(ar), 1 To 1)
If Trim(br(i, 1)) <> "" And Trim(br(i, 2)) <> "" Then
zd = Trim(br(i, 1)) & "|" & Trim(br(i, 2))
For s = 2 To UBound(ar)
If Trim(ar(s, 1)) <> "" And Trim(ar(s, 2)) <> "" Then
zf = Trim(ar(s, 1)) & "|" & Trim(ar(s, 2))
If zf = zd Then
n = n + 1
arr(n, 1) = ar(s, 4)
End If
End If
Next s
If n > 0 Then
rr = Split(zd, "|")
If w = vbYes Then
sh.Copy after:=Sheets(Sheets.Count)
With Sheets(Sheets.Count)
.Name = Replace(zd, "|", "")
.[a1] = "某某学校" & rr(0) & "年级" & rr(1) & "班学生体温监测表" & Chr(10) & "日期: 班主任:" & br(i, 3)
If n <= 30 Then
.[b4].Resize(n, 1) = arr
ElseIf n > 30 Then
hh = 3
lh = 2
For s = 1 To 30
hh = hh + 1
.Cells(hh, lh) = arr(s, 1)
Next s
hh = 3
lh = 8
For s = 31 To n
hh = hh + 1
.Cells(hh, lh) = arr(s, 1)
Next s
End If
.[h33] = br(i, 3)
'.PrintOut
End With
ElseIf w = vbNo Then
sh.Copy
Set wb = ActiveWorkbook
With wb.Worksheets(1)
.Name = Replace(zd, "|", "")
.[a1] = "某某学校" & rr(0) & "年级" & rr(1) & "班学生体温监测表" & Chr(10) & "日期: 班主任:" & br(i, 3)
If n <= 30 Then
.[b4].Resize(n, 1) = arr
ElseIf n > 30 Then
hh = 3
lh = 2
For s = 1 To 30
hh = hh + 1
.Cells(hh, lh) = arr(s, 1)
Next s
hh = 3
lh = 8
For s = 31 To n
hh = hh + 1
.Cells(hh, lh) = arr(s, 1)
Next s
End If
.[h33] = br(i, 3)
End With
wb.SaveAs Filename:=ThisWorkbook.Path & "\" & Replace(zd, "|", "") & ".xlsx"
wb.Close
End If
End If
End If
Next i
Application.ScreenUpdating = True
MsgBox "拆分完毕!", 64, "提醒!"
End Sub
|
|