我有這個基於工作的項目,VBA會自動將選擇列複製到不同工作簿中的某些工作表。VBA將Excel列複製到不同的工作簿並關閉文件
Sub Macro2()
MsgBox "It's working 2"
Dim incidentDataColumnC As Range, SpecificDataColumnA As Range
Set incidentDataColumnC = Workbooks("01 - January 2017 Incident Data.csv").Worksheets(1).Columns("C")
Set SpecificDataColumnA = Workbooks.Open("Z:\****\Real Test\01 - January 2017 Service Desk Reports.xlsx").Worksheets(3).Columns("A")
incidentDataColumnC.Copy Destination:=SpecificDataColumnA
SpecificDataColumnA.Parent.Close
Dim incidentDataColumnE As Range, SpecificDataColumnB As Range
Set incidentDataColumnE = Workbooks("01 - January 2017 Incident Data.csv").Worksheets(1).Columns("E")
Set SpecificDataColumnB = Workbooks.Open("Z:\****\Real Test\01 - January 2017 Service Desk Reports.xlsx").Worksheets(3).Columns("B")
incidentDataColumnE.Copy Destination:=SpecificDataColumnB
SpecificDataColumnB.Close
Dim incidentDataColumnH As Range, SpecificDataColumnC As Range
Set incidentDataColumnH = Workbooks("01 - January 2017 Incident Data.csv").Worksheets(1).Columns("H")
Set SpecificDataColumnC = Workbooks.Open("Z:\****\Real Test\01 - January 2017 Service Desk Reports.xlsx").Worksheets(3).Columns("C")
incidentDataColumnH.Copy Destination:=SpecificDataColumnC
SpecificDataColumnC.Close
....
....
MsgBox "It ran"
End Sub
該代碼的功能應該是通過複製列的方式,但它返回一個錯誤,說明目標文件已打開。
我想找到解決此錯誤的方法,並且如果可能在複製過程運行後關閉每個文件。
我嘗試了一個簡單的.Close命令,但彈出一個錯誤,說它不受支持。
簡單的答案是打開工作簿一次,而不是三次。 – YowE3K
請聲明一些對象變量並回復給我們:https://msdn.microsoft.com/en-us/library/office/gg251791.aspx – Absinthe
'SpecificDataColumnC'是'Range',你不能'SpecificDataColumnC .Close',同上'SpecificDataColumnB'。另外,請參閱上文。 –