0

以下宏在 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

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

4

3 回答 3

2

好吧,没有关于变量内容的详细信息,我只是猜测它是 Windows 和 Mac 之间的文件位置 URI 差异......里面有什么Sheets("Control").Range("C2")

于 2013-01-07T04:45:27.387 回答
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
于 2013-01-05T14:01:38.813 回答
0

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

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

于 2017-03-29T08:08:43.247 回答