下面的代码每隔9998行选择一次,创建一个新的Excel工作表,然后粘贴内容。
1)我需要给所有新的Excel文件一个通用的标题。单元格值应从A2开始。
2)我不想选择整行,仅选择A列和C列。
3)我想在已创建的新Excel工作簿中更改工作表名称。
Sub test()
Dim lastRow As Long, myRow As Long, myBook As Workbook
lastRow = ThisWorkbook.Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
For myRow = 2 To lastRow Step 9998
Set myBook = Workbooks.Add
ThisWorkbook.Sheets("Sheet1").Rows(myRow & ":" & myRow +9997).EntireRow.Copy myBook.Sheets("Sheet1").Range("A1")
Next myRow
End Sub
答案 0 :(得分:0)
尝试以下代码:
Sub test()
Dim lastRow As Long, myRow As Long, myBook As Workbook
With ThisWorkbook.Sheets("Sheet1")
lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
For myRow = 2 To lastRow Step 9998
Set myBook = Workbooks.Add
.Range("A" & myRow & ":C" & myRow + 9997).Copy myBook.Sheets(1).Range("A2")
ActiveWorkbook.Sheets(1).Name = "New Name"
Next myRow
End With
End Sub
1)通过将粘贴范围更改为.Range(“ A2”)
即可解决此问题2)我将“复制范围”更改为仅选择A-C列
3)工作表名称更改发生在代码的最后一行(恰好在“ Next myRow”之前)
希望有帮助!