【问题标题】:How to split spreadsheet into multiple spreadsheets with set number of rows?如何将电子表格拆分为具有设定行数的多个电子表格?
【发布时间】:2013-08-02 14:24:08
【问题描述】:

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

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

【问题讨论】:

  • 在纯 Excel 中只是手工工作。你想要 VBA 吗?

标签: excel excel-2007


【解决方案1】:

我为 .xlsx 文件格式更新了 @Mohamed Sami 的代码。

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 = 11                   '10 rows and 1 header

  '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 "MyTest" & WorkbookCounter & ".xlsx", FileFormat:=51
    wb.Close

    'Increment file counter
    WorkbookCounter = WorkbookCounter + 1
  Next p

  Application.ScreenUpdating = True
  Set wb = Nothing
End Sub

要执行此代码:

  1. 打开工作表
  2. 按 alt+f11(Windows)
  3. 右键单击工作表
  4. 选择插入模块
  5. 粘贴上面的代码
  6. 点击代码中的任意一行
  7. 点击绿色(播放)按钮执行代码

您的文件将保存在 Documents 文件夹中。

【讨论】:

    【解决方案2】:

    我将@Fer Garcia 的代码更新给 Mac 用户 ;),仅更改文件保存方法

    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 = 150                   '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 "Test" & WorkbookCounter & ".xls", FileFormat:=57
        wb.Close
    
        'Increment file counter
        WorkbookCounter = WorkbookCounter + 1
      Next p
    
      Application.ScreenUpdating = True
      Set wb = Nothing
    End Sub
    

    【讨论】:

      【解决方案3】:

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

      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
      

      希望这会有所帮助。

      【讨论】:

      • 甜!!节省了我将 345,000 行文件拆分为每个 10,000 行的文件的时间。谢谢!
      • 该代码按预期工作。感谢您提供如此简洁易懂的代码。
      猜你喜欢
      • 1970-01-01
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      相关资源
      最近更新 更多