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

运行以上代码

效果图


相关推荐
蒋胜山6 小时前
Excel 练习题(7)
经验分享·excel
蒋胜山9 小时前
Excel 练习题(6)
经验分享·excel
wcy_101110 小时前
QCoder智能生成Excel数据清洗与可视化代码
python·excel
JoshRen1 天前
2026教程:上传Excel,用Gemini 3镜像站多模态一键生成问卷分析图表代码与结论(附国内免费方案)
excel
实战编程2 天前
Temu 插件导出 Excel 图片问题总结(SheetJS / ExcelJS)
excel
Data-Miner2 天前
用DeepSeek V4做表:数以轻舟Agent让做Excel表像聊天一样简单
microsoft·excel
Eiceblue3 天前
使用 C# 将 Excel 转换为 Markdown 表格(含批量转换示例)
开发语言·c#·excel
Java面试题总结3 天前
使用 Python 设置 Excel 数据验证
开发语言·python·excel
追逐梦想永不停3 天前
记录一个好用的excel判断数字格式的公式
前端·chrome·excel
Eiceblue3 天前
C# 如何实现 Word 转 Excel ?分享两种实用方法
c#·word·excel