2012-11-05 267 views
1

我正在嘗試將特定的工作表從一個已關閉的工作簿複製到一個特定工作表到我打開的工作簿,但我收到一個運行時錯誤。VBA - 運行時錯誤1004

的代碼如下:

Option Explicit 

Sub START() 


'Defining variables as a worksheet or workbook 
Dim shBrandPivot As Worksheet 
Dim shCurrentWeek As Worksheet 
Dim shPriorWeek As Worksheet 
Dim shPivot As Worksheet 
Dim wkb As Workbook 
Dim wkbFrom As Workbook 
Dim wks As Worksheet 
Dim rng As Range 
Dim path As String, FilePart As String 
Dim TheFile 
Dim loc As String 

'Setting sheets to active or to recognize where to pull information 
Set shBrandPivot = ActiveWorkbook.Sheets("Brand Pivot") 
Set shCurrentWeek = ActiveWorkbook.Sheets("Current Week") 
Set shPriorWeek = ActiveWorkbook.Sheets("Prior Week") 
Set shPivot = ActiveWorkbook.Sheets("Pivot") 
Set wkb = ThisWorkbook 
loc = shPivot.Range("E11").Value 
path = shPivot.Range("E12").Value 
FilePart = Trim(shPivot.Range("E13").Value) 
TheFile = Dir(path & "*" & FilePart & ".xls") 
Set wkbFrom = Workbooks.Open(loc & path & TheFile) 
Set wks = wkbFrom.Sheets("SUPPLIER_01_00028257_KIK CUSTOM") 
Set rng = wks.Range("A2:N500") 

'Copy and pastes This week number to last week number 
shPivot.Range("E22:E23").Copy shPivot.Range("F22") 

'Deletes necessary rows in PriorWeek tab 
shPriorWeek.Range("A4:AC1000").Delete 

'Copies necessary rows in CurrentWeek tab to PriorWeek tab 
shCurrentWeek.Range("A4:AC1000").Copy shPriorWeek.Range("A4") 

'Copies range from report generated to share drive and pastes into the current week tab of open order report 
rng.Copy wkb.Sheets("Current Week").Range("A4") 

'Closes the workbook in the shared drive 
wkbFrom.Close False 

End Sub 

這是什麼,是在細胞E11,E12和E13分別爲:

E11 - S:_Supply鏈\週刊RPTS供應商和買家\ E12 - 102112 E13 - SUPPLIER_01_00028257_KIK定製產品GAINSVILLE_21-OCT-12.xls

我想打開關閉的工作簿的目錄爲S:_Supply鏈\週刊RPTS供應商及買家\ 102112 \ Supplier_01_00028257_KIK CUSTOM PROD UCTS GAINSVILLE_21-OCT-12.xls

回答

2

我建議你閱讀this

因爲當你給工作簿定義名稱和 然後複製工作表了幾次都沒有第一

可能會出現此問題保存並關閉 工作簿

如何解決

要解決此問題,保存並定期關閉工作簿 而複製過程中發生

。討論的在聊天

摘要通過設置一個斷點Set wkbFrom = Workbooks.Open(loc & path & TheFile)

我們意識到文件名缺失原因Dir(path & "*" & FilePart & ".xls") 返回沒有文件名的目錄。

因此,解決方案是增加其在細胞E13

+0

嗯,我不認爲這是問題。在運行時錯誤後,我收到消息「Excel can not access'102112'。文檔可能是隻讀或加密的,文檔既不是這種情況。 – kmiao91

+0

您是否確認可以正常打開文檔? – Marc

+0

確認我可以打開文檔,但是我收到一條消息:「您要打開的文件」此處的文件名「的格式與文件擴展名指定的格式不同。你還想打開嗎?驗證此文件來自可信來源。 – kmiao91

1

我做這之前可能是有用的給你類似的事情的文件名。我只複製一個範圍,但您可以輕鬆地將範圍的大小更改爲希望複製的工作表上已用空間的數量。

Dim source as Workbook 
Dim dest As Workbook 
Dim originalWorkBook As Workbook 
Set originalWorkBook = ThisWorkbook 
Dim ws As Worksheet 
Dim copyRange As String 
Dim myDir As String 
'myDir is found via a function I wrote. The user enters a name, month and year and 
'from there it knows where the file will be 

copyRange = "A1:D80" 

source.Worksheets("The name of your worksheet here").Range(copyRange).Copy 
originalWorkBook.Activate 
originalWorkBook.Worksheets("Sheet1").Range("A1").Select 
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _ 
False, Transpose:=False 
source.Close False 
Application.ScreenUpdating = True 

希望這會有所幫助。