码迷,mamicode.com
首页 > 其他好文 > 详细

按照空行拆分表格并保存为工作簿

时间:2020-12-08 12:43:24      阅读:4      评论:0      收藏:0      [点我收藏+]

标签:str   false   end   sel   复制表   close   count   this   handler   

Sub 插入()

On Error GoTo errHandler

Dim i As Long ‘按照标题插入空行
For i = 2 To Range("A1").CurrentRegion.Count - 1
Selection.End(xlDown).Select
Selection.EntireRow.Insert , CopyOrigin:=xlFormatFromLeftOrAbove
ActiveCell.Offset(1, 0).Range("A1").Select
Next
errHandler:

Dim rng As Range ‘分割表格
Range("A1:O" & Range("B65536").End(xlUp).Row).AutoFilter Field:=2, Criteria1:="<>"
For Each rng In Range("A2:O" & Range("B65536").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Areas
Worksheets.Add after:=Worksheets(Worksheets.Count)
rng.Copy ActiveSheet.Range("A1")
Next rng
Range("A1:O" & Range("B65536").End(xlUp).Row).AutoFilter

Dim k As Integer ‘复制表头
For k = 1 To 39
Sheets("汇总").Select
Rows("1:1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet" & k).Select
Rows("1:1").Select
Selection.Insert Shift:=xlDown
ActiveSheet.Paste
Next

Dim x As Integer, sht As Worksheet ‘表格改名
x = 1
For Each sht In Worksheets
If sht.Name <> "汇总" And sht.Name <> "11" Then
sht.Name = Left(sht.Cells(2, 15).Value, Len(sht.Cells(2, 15).Value) - 5)
x = x + 1
End If
Next
On Error Resume Next ‘
Dim FolderPath As String, FolderName As String, BN As String
Dim ReturnValue As Integer

Dim sht As Worksheet
Dim file_name$
For Each sht In Sheets
sht.Copy
file_name = ThisWorkbook.path & "\" & sht.Name & ".xlsx"
ActiveWorkbook.SaveAs Filename:=file_name, FileFormat _
:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
Next
End Sub

按照空行拆分表格并保存为工作簿

标签:str   false   end   sel   复制表   close   count   this   handler   

原文地址:https://www.cnblogs.com/yukit/p/14083996.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!