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

运行以上代码

效果图


相关推荐
weixin_493503673 小时前
商会系统里的在线文件预览:PDF/Word/Excel 三种实现与选型(Node + 前端实战)
pdf·word·excel
asdzx675 小时前
Python 解析 Excel 数据、图片与图表的实现方案
python·excel
慧都小妮子6 小时前
GcExcel 服务端 Excel 组件:无需安装 Office 的 Java/.NET 报表方案
java·.net·excel·gcexcel·服务端excel组件
The Future is mine19 小时前
Excel如何只允许对部分表格内容进行修改
excel
2601_9620994620 小时前
Python xlwt设置excel单元格字体及格式
python·excel·xlwt·样式设置·单元格格式
葡萄城技术团队1 天前
不会写 SUMIFS 也能做汇总?用 SpreadJS AI 生成并解释 Excel 公式
人工智能·excel
2501_933670791 天前
采购运营校招Excel能力清单:函数、透视表、ERP数据与SQL入门
数据库·sql·excel
netho02 天前
影刀RPA初级证书+ Excel 全自动数据看板
excel·rpa
2501_930707782 天前
使用C#代码设置 Excel 单元格的背景颜色和图案
excel
ms365copilot2 天前
Excel Copilot聊天历史|关闭文件后,还能找回之前的AI分析对话
人工智能·excel·copilot