简体   繁体   中英

VBA opening/closing workbooks on MAC throws error

The following macro has no problems on windows but throws a runtime error on a MAC. The problem is that I do not have a MAC to test this on. The code simply picks up the locations of 2 files (A & B) from cells on another sheet, opens them, copies the data into another sheet and does some calculations.

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

A version where the data is already in the same workbook on other sheets works fine. So it must be opening/closing/switching between the workbooks that isn't working.

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

Try fully qualifying everything and declaring all vaiables - see if this makes things more obvious to the processor:

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

Depends on 2011 (: as separator) vs 2016 (Posix path) Office for Mac

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM