将Excel VB宏转换为Mac兼容

时间:2016-04-05 19:29:24

标签: excel macos vba excel-vba phpexcel

我有以下VB宏用于获取大型Excel文件并将其拆分为许多较小的文件,每个文件由1,000行组成(999个数据,1个标题行)。

Sub SplitExcelFile()
  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 = 1000                  '1000 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 & "/File_" & WorkbookCounter & ".xls", FileFormat:=xlWorkbookNormal
    wb.Close

    'Increment file counter
    WorkbookCounter = WorkbookCounter + 1
  Next p

  Application.ScreenUpdating = True
  Set wb = Nothing
End Sub

它在PC上完美运行,但是当我在Mac上运行时,它表示关于未定义对象的错误。所以我认为一定是路径分隔符不是“\”,必须是“/”。所以我改变了它似乎运行正常。它至少创建了文件。但是,当我尝试在PC上打开它时,它显示错误,说明以下内容:

The file format and extension of 'File_1.xls' don't match.  The file could be corrupted or unsafe.  Unless you trust its source, don't open it.  Do you want to open it anyway?

当我尝试在使用它的网站上使用PHPExcel打开它时会出现问题。关于为什么会发生这种情况以及我如何纠正它的任何想法?

0 个答案:

没有答案
相关问题