excel 每隔5,000行迭代地将数据文件拆分为模板文件并另存为新文件

xnifntxz  于 2022-12-01  发布在  其他
关注(0)|答案(1)|浏览(104)

因限制而移除因限制而移除

gr8qqesn

gr8qqesn1#

请尝试以下操作:

Sub test()
    Const BLOCK_SIZE As Long = 5000
    Dim wsSrc As Worksheet, myBook As Workbook, rngCopy As Range
    
    Set wsSrc = ActiveSheet 'or some other specific sheet
    Set rngCopy = wsSrc.Rows(2).Resize(BLOCK_SIZE)
    Do While Application.CountA(rngCopy) > 0  'loop while range has content
        With Workbooks.Open("/Users/Downloads/Test/TemplateFile.xlsx") 
            rngCopy.Copy .Worksheets("Sheet1").Range("A2")
            .SaveAs "\Users\Downloads\Test\" & "R" & rngCopy.Row & "_" & .Name
            .Close SaveChanges:=True
        End With
        Set rngCopy = rngCopy.Offset(BLOCK_SIZE) 'next block down
    Loop
End Sub

相关问题