导入文件可能会有很多行,可能超过5000行。

huangapple go评论60阅读模式
英文:

While import file will have many row maybe more than 5000 rows

问题

在导入文件时可能会有很多行,可能超过5000行,因此我希望将文件拆分为多个Excel文件,第一个文件包含1-100行,第二个文件包含101-201行,然后循环处理文件直到处理完5000行。

Sub test()
Sheet60.Range("A1:BX100").Copy
Workbooks.Add
Application.DisplayAlerts = False
ActiveSheet.Paste Destination:=Range("A1")
ActiveWorkbook.SaveAs ("D:\Implement\text.xlsx")
ActiveWorkbook.Close
Application.DisplayAlerts = True
End Sub
英文:

While import file will have many row maybe more than 5000 row, so I want export files to cut 1-100 row to in 1 excel and then 101-201​ to another file and then loop file until to 5000 row.

Sub test()
Sheet60.Range("A1:BX100").Copy
Workbooks.Add
Application.DisplayAlerts = False
ActiveSheet.Paste Destination:=Range("A1")
ActiveWorkbook.SaveAs ("D:\Implement\text.xlsx")
ActiveWorkbook.Close
Application.DisplayAlerts = True
End sub

答案1

得分: 2

逐步执行循环,我使用了Sheet60,因为这是你在你的代码中使用的。

Sub MakeWBs()
    Dim dr As String
    Dim ws As Worksheet
    Dim LstRw As Long, x As Long
    Dim Nsh As Worksheet

    dr = "C:\TestNewFolder"                      'change to your folder
    Set ws = Sheet60
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    With ws
        LstRw = .Cells(.Rows.Count, "A").End(xlUp).Row
        For x = 1 To LstRw Step 100             'step 100 next loop goes to 101 ect
            Set Nsh = Sheets.Add(After:=Sheets(Sheets.Count))
            Nsh.Name = x
            .Range(.Cells(x, 1), .Cells(x + 99, 76)).Copy Nsh.Range("A1")
            Nsh.Copy
            ActiveWorkbook.SaveAs Filename:=dr & "\" & Nsh.Name & ".xlsx", FileFormat:=51
            ActiveWorkbook.Close
            Nsh.Delete
        Next x
    End With

    MsgBox "done"
End Sub
英文:

Step through the loop, I used Sheet60 because that is what you used in your code.

    Sub MakeWBs()
    Dim dr As String
    Dim ws As Worksheet
    Dim LstRw As Long, x As Long
    Dim Nsh As Worksheet

    dr = "C:\TestNewFolder"                      'change to your folder
    Set ws = Sheet60
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    With ws
        LstRw = .Cells(.Rows.Count, "A").End(xlUp).Row
        For x = 1 To LstRw Step 100             'step 100 next loop goes to 101 ect
            Set Nsh = Sheets.Add(After:=Sheets(Sheets.Count))
            Nsh.Name = x
            .Range(.Cells(x, 1), .Cells(x + 99, 76)).Copy Nsh.Range("A1")
            Nsh.Copy
            ActiveWorkbook.SaveAs Filename:=dr & "\" & Nsh.Name & ".xlsx", FileFormat:=51
            ActiveWorkbook.Close
            Nsh.Delete
        Next x
    End With

    MsgBox "done"
End Sub

huangapple
  • 本文由 发表于 2023年6月16日 11:24:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定