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
相关推荐
Tomorrow'sThinker18 小时前
[特殊字符] Python 批量生成词云:读取词频 Excel + 自定义背景 + Excel to.png 流程解析
python·excel
UrbanJazzerati1 天前
Excel 使用中的“坑”:拆分与合并列的陷阱及解决方案
excel
KeThink1 天前
国民经济行业分类 GB/T 4754—2017 (PDF和exce版本)
pdf·excel
_oP_i1 天前
Excel 的多线程特性
excel
V1ncent Chen2 天前
Excel基础:数据查看
excel
谁他个天昏地暗2 天前
Java 实现 Excel 文件对比与数据填充
java·开发语言·excel
梦想blog2 天前
Spring Boot + Easy Excel 自定义复杂样式导入导出
excel
UrbanJazzerati3 天前
使用Excel制作多类别占比分析字母饼图
excel
The Future is mine3 天前
Python实现文件夹中文件名与Excel中存在的文件名进行对比,并进行删除操作
excel
Tomorrow'sThinker3 天前
[特殊字符] Excel 读取收件人 + Outlook 批量发送带附件邮件 —— Python 自动化实战
python·excel·outlook