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
相关推荐
云只上7 小时前
前端插件使用xlsx-populate,花样配置excel内容,根据坐添加标替换excel内容,修改颜色,合并单元格...。
excel
小哥山水之间8 小时前
在 Python 中操作 Excel 文件
开发语言·python·excel
blog_wanghao9 小时前
C#: 创建Excel文件并在Excel中写入数据库中的数据
数据库·c#·excel
游客52016 小时前
自动化办公|通过xlwings进行excel格式设置
python·自动化·excel
喝西瓜汁的兔叽Yan17 小时前
第三方库XLSX: 前端上传excel文件,并对excel文件内容做校验。
前端·excel·xlsx
刘_sy1 天前
使用EasyExcel和多线程实现高效数据导出
java·excel·easyexcel·批量导出excel
PowerBI学谦2 天前
Copilot:Excel中的Python高级分析来了
python·excel·copilot
E-iceblue3 天前
Python 合并 Excel 单元格
python·excel·合并
sszdzq3 天前
Excel 合并列数据
excel
prince_zxill3 天前
Excel中不用复杂公式根据指定X列的数值N复制整行数据N行简单方法
excel