Excel按固定行数拆分为多个Excel

步骤 1

打开VBA编辑器

打开你的Excel文件。

按 Alt + F11 打开VBA编辑器。

在VBA编辑器中,点击 插入 -> 模块,创建一个新的模块。

步骤 2:

编写VBA代码

在新创建的模块中输入以下VBA代码:

xml 复制代码
Sub SplitWorkbookByRows()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets(1) '假设要拆分的工作表是第一个工作表
    Dim totalRows As Long
    totalRows = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row '获取总行数
    Dim rowsPerFile As Long
    rowsPerFile = InputBox("请输入每个文件包含的行数:") '用户输入每份文件的行数
    Dim fileCounter As Integer
    fileCounter = 1
    Dim startRow As Long
    startRow = 2 '假设第一行是标题行,从第二行开始拆分
    Dim endRow As Long
    Dim newWb As Workbook
    
    Do While startRow <= totalRows
        endRow = startRow + rowsPerFile - 1
        If endRow > totalRows Then
            endRow = totalRows
        End If
        
        '复制数据到新工作簿
        Set newWb = Workbooks.Add
        ws.Rows("1:1").Copy Destination:=newWb.Sheets(1).Range("A1") '复制标题行
        ws.Rows(startRow & ":" & endRow).Copy Destination:=newWb.Sheets(1).Range("A2")
        
        '保存新工作簿
        newWb.SaveAs ThisWorkbook.Path & "\Split_" & fileCounter & ".xlsx"
        newWb.Close
        
        startRow = endRow + 1
        fileCounter = fileCounter + 1
    Loop
    
    MsgBox "拆分完成!"
End Sub

运行以上代码

效果图


相关推荐
fs哆哆1 天前
VB.NET和VBA教程-如何查找Excel数据区域边界
excel
小矮强1 天前
Excel中根据年月日提取月日,并按月日进行排序
excel
开开心心_Every1 天前
图片批量压缩工具:支持有损无损两种模式
python·游戏·微信·django·pdf·excel·语音识别
wtsolutions1 天前
Real-World Use Cases - How Organizations Use JSON to Excel
json·excel
wtsolutions1 天前
Introduction to JSON to Excel - The Ultimate Conversion Tool
json·excel
骆驼爱记录1 天前
Python打包命令全攻略
自动化·word·excel·新人首发
wtsolutions1 天前
JSON to Excel WPS Add-in - Perfect for WPS Office Users
json·excel·wps
hhzz1 天前
Springboot项目中使用EasyPOI操作Excel(详细教程系列4/4)
java·spring boot·后端·spring·excel·poi·easypoi
不坑老师2 天前
小工具显出大才能——不坑盒子为教育数字化转型贡献“新方案”
microsoft·word·excel·ppt·office
骆驼爱记录2 天前
Python程序打包全攻略
自动化·word·excel·wps·新人首发