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

运行以上代码

效果图


相关推荐
天一生水water5 小时前
顶级多项目管理 Excel 模板
excel·敏捷流程
取啥都被占用6 小时前
VBA的excel逐行替换到word模板及打印还原
excel·vba
坚定信念,勇往无前7 小时前
vue3图片,pdf,word,excel,ppt多格式文件预览组件Vue Doc Viewers Plus
pdf·word·excel
weixin_4624462317 小时前
【原创实践】python 获取节假日列表 并保存为excel
数据库·python·excel
缺点内向1 天前
如何在C#中添加Excel文档属性?
开发语言·数据库·c#·.net·excel
shouchaobao1 天前
仓库房进销存Excel模板合集:商品采购+出入库+库存统计一站式管理,适配仓库管理员/中小企业/个体商户
excel
chenhdowue1 天前
如何使用 vxe-table 导出为带图片的单元格到 excel 格式文件
vue.js·excel·vxe-table·vxe-ui
码上成长1 天前
从零实现:react&Ts--批量导入 & Excel 模版下载功能
javascript·react.js·excel
heartbeat..1 天前
如何用 Excel / Java 去解决Excel下拉列表选项数量限制的问题(兼容.xsl和.xlsx)
java·开发语言·excel·poi
前端sweetGirl1 天前
Excel 里 XLOOKUP 函数返回日期时找不到值显示 1/0,怎么让他不显示
excel