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
相关推荐
办公解码器8 小时前
Excel工作表打开一次后自动销毁文件,回收站中都找不到
数据库·excel
mudtools10 小时前
.NET驾驭Excel之力:工作簿与工作表操作基础
c#·.net·excel
mudtools10 小时前
.NET驾驭Excel之力:单元格与区域操作详解
c#·.net·excel
秋天之落叶14 小时前
使用ADO将excel表内容加载到mssql表中的长度问题
数据库·sqlserver·excel
1314lay_100714 小时前
SAP ABAP 导入excel表,导多个sheet
excel·abap
玩泥巴的17 小时前
.NET驾驭Excel之力:工作簿与工作表操作基础
c#·excel·二次开发·office·com互操作
mysusheng1 天前
2025 批量下载微博内容/图片/视频,导出word和pdf,微博点赞/评论/转发等数据导出excel
pdf·word·excel
办公解码器2 天前
Excel工作簿自动销毁功能,使用时间到期后自动删除文件
excel
MediaTea2 天前
Python 第三方库:OpenPyXL(Excel 文件读写与操作)
开发语言·python·excel
开开心心就好2 天前
无需函数:Excel数据筛选工具推荐
xml·网络·pdf·华为云·word·excel·音视频