gpt4 book ai didi

excel - 如何将电子表格拆分为多个具有设定行数的电子表格?

转载 作者:行者123 更新时间:2023-12-01 17:37:43 25 4
gpt4 key购买 nike

我有一个包含 433 行(加上顶部的标题行)的 Excel (2007) 电子表格。我需要将其拆分为 43 个单独的电子表格文件,每个文件包含 10 行,其中一个包含剩余 3 行。

最好将标题行也放在每个电子表格的顶部。我怎样才能做到这一点?

最佳答案

您的宏只是拆分所选范围内的所有行,包括第一行中的标题行(因此它只会在第一个文件中出现一次)。我根据你的要求修改了宏;这很简单,查看我写的评论看看它的作用。

Sub Test()
Dim wb As Workbook
Dim ThisSheet As Worksheet
Dim NumOfColumns As Integer
Dim RangeToCopy As Range
Dim RangeOfHeader As Range 'data (range) of header row
Dim WorkbookCounter As Integer
Dim RowsInFile 'how many rows (incl. header) in new files?

Application.ScreenUpdating = False

'Initialize data
Set ThisSheet = ThisWorkbook.ActiveSheet
NumOfColumns = ThisSheet.UsedRange.Columns.Count
WorkbookCounter = 1
RowsInFile = 10 'as your example, just 10 rows per file

'Copy the data of the first row (header)
Set RangeOfHeader = ThisSheet.Range(ThisSheet.Cells(1, 1), ThisSheet.Cells(1, NumOfColumns))

For p = 2 To ThisSheet.UsedRange.Rows.Count Step RowsInFile - 1
Set wb = Workbooks.Add

'Paste the header row in new file
RangeOfHeader.Copy wb.Sheets(1).Range("A1")

'Paste the chunk of rows for this file
Set RangeToCopy = ThisSheet.Range(ThisSheet.Cells(p, 1), ThisSheet.Cells(p + RowsInFile - 2, NumOfColumns))
RangeToCopy.Copy wb.Sheets(1).Range("A2")

'Save the new workbook, and close it
wb.SaveAs ThisWorkbook.Path & "\test" & WorkbookCounter
wb.Close

'Increment file counter
WorkbookCounter = WorkbookCounter + 1
Next p

Application.ScreenUpdating = True
Set wb = Nothing
End Sub

希望这有帮助。

关于excel - 如何将电子表格拆分为多个具有设定行数的电子表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17997851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com