Home » excel » excel vba – VBA – Run Time Error 1004

excel vba – VBA – Run Time Error 1004

Posted by: admin May 14, 2020 Leave a comment


I am attempting to Copy a specific sheet from a closed workbook to a specific sheet to my opened workbook but I am receiving a run-time error.

The code is below:

Option Explicit


'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

'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

This is what is in cell E11, E12 and E13 respectively:

E11 – S:_Supply Chain\Weekly Rpts Supplier and Buyer\
E12 – 102112

The directory I wish to open the closed workbook is S:_Supply Chain\Weekly Rpts Supplier and Buyer\102112\Supplier_01_00028257_KIK CUSTOM PRODUCTS GAINSVILLE_21-OCT-12.xls

How to&Answers:

I recommend you read this


This problem can occur when you give the workbook a defined name and
then copy the worksheet several times without first saving and closing
the workbook

How to Resolve

To resolve this problem, save and close the workbook periodically
while the copy process is occurring

For a summary of the discussion in the chat

By setting a breakpoint on Set wkbFrom = Workbooks.Open(loc & path & TheFile)

We realized that the file name was missing cause Dir(path & "*" & FilePart & ".xls")
returns the directory without the file name.

So the solution is to add the file name which is in the cell E13


I’ve done a similar thing before which might be of use to you. Mine only copies a range, but you can easily change the size of the range to the amount of used space on the sheet you wish to copy.

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
Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
source.Close False
Application.ScreenUpdating = True

Hope this helps.