MAC上的VBA打开/关闭工作簿会引发错误

时间:2013-01-05 10:37:29

标签: macos excel vba excel-vba excel-vba-mac

以下宏在Windows上没有问题,但在MAC上引发运行时错误。问题是我没有MAC来测试它。代码只是从另一张纸上的单元格中选取2个文件(A& B)的位置,打开它们,将数据复制到另一张纸上并进行一些计算。

Sub Compile()
Application.ScreenUpdating = False

ThisBook = ActiveWorkbook.Name

'open A
Workbooks.Open (Sheets("Control").Range("C2"))
ABook = ActiveWorkbook.Name

'copy data from A
Workbooks(ThisBook).Sheets("Data").Cells.Clear
ActiveSheet.UsedRange.Copy Workbooks(ThisBook).Sheets("data").Range("A1")

'close A
Workbooks(ThisBook).Activate
Workbooks(ABook).Close False

'prep A
Sheets("data").Activate
C = Range("A" & Cells.Rows.Count).End(xlUp).Row

'some calculation

'open B
Workbooks.Open (Sheets("Control").Range("C5"))
BBook = ActiveWorkbook.Name

'prep B
D = ActiveSheet.Range("A" & Cells.Rows.Count).End(xlUp).Row

'some calculation

'copy B
Range("M2:M" & D).Copy Workbooks(ThisBook).Sheets("data").Range("O2")

'close B
Workbooks(ThisBook).Activate
Workbooks(BBook).Close False

'calculate
Sheets("data").Activate

'some more calculations

Application.ScreenUpdating = True
End Sub

数据已在其他工作表上的同一工作簿中的版本可以正常工作。因此必须在不工作的工作簿之间打开/关闭/切换。

3 个答案:

答案 0 :(得分:2)

没有变量内容和具体内容,我只是猜测它是Windows和Mac之间的文件位置URI差异...... Sheets("Control").Range("C2")中的内容是什么?

答案 1 :(得分:1)

尝试完全限定所有内容并声明所有变量 - 看看这是否会使处理器更加明显:

Sub Compile()
Excel.Application.ScreenUpdating = False

Dim ThisBook As String 
ThisBook = Excel.ActiveWorkbook.Name

'open A
Workbooks.Open (Sheets("Control").Range("C2"))
ABook = ActiveWorkbook.Name

'copy data from A
with Excel.Workbooks(ThisBook)
     .Sheets("Data").Cells.Clear
     .ActiveSheet.UsedRange.Copy .Sheets("data").Range("A1")
end with

'close A
Excel.Workbooks(ThisBook).Activate
Excel.Workbooks(ABook).Close False

'prep A
with Excel.Workbooks(ThisBook).Sheets("data")
    .Activate
    Dim c as integer
    C = .Range("A" & .Rows.Count).End(Excel.xlUp).Row
end with

'some calculation

'open B
Excel.Workbooks.Open (Excel.Workbooks(ThisBook).Sheets("Control").Range("C5"))
dim BBook as string
BBook = Excel.ActiveWorkbook.Name

'prep B
dim D as integer
with Excel.ActiveSheet.
  D = .Range("A" & .Rows.Count).End(Excel.xlUp).Row
end with

'some calculation

'copy B
Range("M2:M" & D).Copy Excel.Workbooks(ThisBook).Sheets("data").Range("O2")

'close B
Excel.Workbooks(ThisBook).Activate
Excel.Workbooks(BBook).Close False

'calculate
Excel.Workbooks(ThisBook).Sheets("data").Activate

'some more calculations

Excel.Application.ScreenUpdating = True
End Sub

答案 2 :(得分:0)

取决于2011年(:作为分隔符)vs 2016(Posix路径)Office for Mac

http://www.rondebruin.nl/mac/mac017.htm