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
相关推荐
QQ35967734519 小时前
ArcGIS Pro实现基于 Excel 表格批量创建标准地理数据库(GDB)——高效数据库建库解决方案
数据库·arcgis·excel
星空的资源小屋2 天前
Digital Clock 4,一款免费的个性化桌面数字时钟
stm32·单片机·嵌入式硬件·电脑·excel
揭老师高效办公3 天前
在Excel和WPS表格中批量删除数据区域的批注
excel·wps表格
我是zxb3 天前
EasyExcel:快速读写Excel的工具类
数据库·oracle·excel
辣香牛肉面3 天前
[Windows] 搜索文本2.6.2(从word、wps、excel、pdf和txt文件中查找文本的工具)
word·excel·wps·搜索文本
ljf88383 天前
Java导出复杂excel,自定义excel导出
java·开发语言·excel
tebukaopu1483 天前
json文件转excel
json·excel
shizidushu3 天前
How to work with merged cells in Excel with `openpyxl` in Python?
python·microsoft·excel·openpyxl
Eiceblue4 天前
使用 C# 设置 Excel 单元格格式
开发语言·后端·c#·.net·excel
acaad4 天前
Apache Poi 实现导出excel表格 合并区域边框未完全显示的问题
spring·apache·excel