Excel按照固定行数拆分为多个文件
Excel按照固定行数拆分为多个文件
工作需要,需要把Excel按照固定行数拆分,生成多个Excel。
1. 打开Excel
鼠标不要任何选择。按“Alt”+“f11”。
2. 粘贴代码
在sheet1贴上以下代码,WJhangshu = 250 ,我们是按照250行来拆分一个的。
Sub cfb()
Dim r, c, i, WJhangshu, WJshu, bt As Long
r = Range("A" & Rows.Count).End(xlUp).Row
c = Cells(1, Columns.Count).End(xlToLeft).Column
bt = 1 'title
WJhangshu = 250 'num
WJshu = IIf(r - bt Mod 20000, Int((r - bt) / WJhangshu), Int((r - bt) / WJhangshu) + 1)
For i = 0 To WJshu
Workbooks.Add
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\" & Format(i, String(Len(WJshu), 0)) & ".xlsx"
Application.DisplayAlerts = True
ThisWorkbook.ActiveSheet.Range("A1").Resize(bt, c).Copy ActiveSheet.Range("A1")
ThisWorkbook.ActiveSheet.Range("A" & bt + i * WJhangshu + 1).Resize(WJhangshu, c).Copy _
ActiveSheet.Range("A" & bt + 1)
ActiveWorkbook.Close True
Next
End Sub
3. 执行
切回Excel,Alt+f8.弹出框选择刚才的宏,可以“执行”或者单步执行。