2013-01-05 150 views
0

以下宏在Windows上沒有問題,但在MAC上拋出運行時錯誤。問題是我沒有MAC來測試它。代碼只是從另一張紙上的單元格中拾取2個文件(A & B)的位置,打開它們,將數據複製到另一張紙上並進行一些計算。MAC上的VBA打開/關閉工作簿拋出錯誤

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 

數據已經在其他工作表上的相同工作簿中的版本可以正常工作。所以它必須打開/關閉/切換不工作的工作簿。

+1

哪條線是給你的錯誤,你得到什麼錯誤? –

+0

我同意@SiddharthRout ...您需要提供有關錯誤的更多特定信息 – whytheq

+0

直到現在,我剛剛被告知其運行時錯誤。我已經問過爲我測試這個具體細節的人。我會分享一次,我有他們。謝謝。 – MarioTheHedgehog

回答

1

嘗試完全限定的一切,並宣佈所有vaiables - 看看這使事情給處理器更爲明顯:

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 
+0

嗯值得一試。我會嘗試並回來。但是像這樣跳轉的工作簿能在Mac上工作嗎? – MarioTheHedgehog

+0

- 我完全不知道MAC上的工作原理(在Excel-VBA中)。我沒有意識到它的任何工作:) – whytheq

2

良好,沒有和你的變量的內容細節,我只是猜測,它是Windows和Mac之間的文件位置URI區別...什麼在Sheets("Control").Range("C2")

+0

+1值得檢查可能是問題。 – whytheq

相關問題