VBA一个sheet多行按照固定行数分割到多个excel中

vbscript 复制代码
Sub SplitWorkbook()

    Dim SourceSheet As Worksheet
    Set SourceSheet = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name

    Dim CurrentRow As Long
    Dim StartRow As Long
    Dim EndRow As Long
    Dim TargetWorkbook As Workbook
    Dim TargetSheet As Worksheet
    Dim Index As Integer
    
    CurrentRow = 2 ' Assuming row 1 has headers
    StartRow = 2
    Index = 1
    
    While CurrentRow <= SourceSheet.UsedRange.Rows.Count
        
        Set TargetWorkbook = Application.Workbooks.Add
        Set TargetSheet = TargetWorkbook.Sheets(1)
        
        If CurrentRow + 499 > SourceSheet.UsedRange.Rows.Count Then
            EndRow = SourceSheet.UsedRange.Rows.Count
        Else
            EndRow = CurrentRow + 499
        End If
        
        SourceSheet.Rows(1).Copy TargetSheet.Rows(1)
        SourceSheet.Rows(StartRow & ":" & EndRow).Copy TargetSheet.Rows(2)
        TargetWorkbook.SaveAs "D:\Temp\File_" & Index & ".xlsx" ' change the file path as needed
        TargetWorkbook.Close SaveChanges:=True
        
        CurrentRow = EndRow + 1
        StartRow = CurrentRow
        Index = Index + 1
        
    Wend

End Sub
相关推荐
青涩小鱼10 小时前
Excel表格转换成PDF文件时显示不全怎么处理?
pdf·excel
每一天,每一步12 小时前
react antd点击table单元格文字下载指定的excel路径
前端·react.js·excel
花开盛夏^.^1 天前
Excel常用功能总结
excel
Excel_easy1 天前
WPS按双字段拆分工作表到独立工作簿-Excel易用宝
excel·wps
JavaNice哥2 天前
easyexcel读取写入excel easyexceldemo
excel
Johaden2 天前
EXCEL+Python搞定数据处理(第一部分:Python入门-第2章:开发环境)
开发语言·vscode·python·conda·excel
进击的雷神2 天前
Excel 实现文本拼接方法
excel
东京老树根2 天前
Excel 技巧15 - 在Excel中抠图头像,换背景色(★★)
笔记·学习·excel
规划GIS会2 天前
CC工具箱使用指南:【Excel点集转面要素(批量)】
excel·二次开发·arcgis pro
东京老树根2 天前
Excel 技巧17 - 如何计算倒计时,并添加该倒计时的数据条(★)
笔记·学习·excel