英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论