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

运行以上代码

效果图


相关推荐
Metaphor69217 小时前
使用 Python 在 Excel 文件中查找并替换
python·excel
xiaomin-Michael17 小时前
EXCEL pivot table
excel
包子源18 小时前
PDF 转 Word/Excel 全链路实战:Next.js + 阿里云文档智能
pdf·word·excel
Java知识技术分享18 小时前
excel动态表头实现+异步使用
java·spring·excel
tedcloud1231 天前
academic-research-skills部署教程:构建AI辅助科研环境
服务器·人工智能·word·excel·dreamweaver
wtsolutions1 天前
Sheet-to-Doc Excel Add-in Released, Install Directly from Microsoft AppSource
microsoft·excel
开开心心就好2 天前
完美兼容Office格式的免费办公套件
windows·均值算法·计算机外设·word·excel·csdn开发云·图搜索算法
未来之窗软件服务2 天前
Excel 物流货运记账表模板【万象EXCEL(二十七)】—东方仙盟
excel·仙盟创梦ide·东方仙盟·万象excel
Land03292 天前
Excel 自动化 RPA 教程:批量处理、报表生成与数据汇总实战,离线方案详解
自动化·excel·rpa