【问题标题】:How do I make this Excel macro save in .xls (Excel 97-2003) format, instead of .xlsx?如何将此 Excel 宏保存为 .xls (Excel 97-2003) 格式,而不是 .xlsx?
【发布时间】:2019-03-26 10:11:21
【问题描述】:

这个宏非常接近我所需要的: How to split spreadsheet into multiple spreadsheets with set number of rows?

它会根据行数将长 Excel 文件拆分为较小的文件。

但是,我需要修改它以将文件保存为 .xls (Excel 97-2003) 格式。

我需要改变什么?

这是我现在正在使用的:

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 = 100                   '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 & "\splitoutput" & WorkbookCounter
    wb.Close

    'Increment file counter
    WorkbookCounter = WorkbookCounter + 1
  Next p

  Application.ScreenUpdating = True
  Set wb = Nothing
End Sub

【问题讨论】:

  • Workbook.SaveAsXlFileFormat enumeration 文档应该可以满足您的需求。
  • 我知道我可以在哪里获得 FileFormat 的变量值和数字格式。我无法将它们插入宏而不破坏它。
  • 实际上,我认为这行得通。我刚刚更改了这个: wb.SaveAs ThisWorkbook.Path & "\splitoutput" & WorkbookCounter 到这个: wb.SaveAs Filename:=ThisWorkbook.Path & "\splitoutput" & WorkbookCounter, _ FileFormat:=xlExcel8 并且成功了。

标签: excel vba


【解决方案1】:

Workbook.SaveAsxlFileFormat enumeration 文档在这里很有帮助。

改变

wb.SaveAs ThisWorkbook.Path & "\splitoutput" & WorkbookCounter

wb.SaveAs FileName:=ThisWorkbook.Path & "\splitoutput" & WorkbookCounter, FileFormat:=xlWorkbookNormal

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-25
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    相关资源
    最近更新 更多