Hi,
I'm beating my head against a brick wall on this one. I've got two macros that each open a (different) file and import data into the source workbook. The source workbook was developed on a shared network drive, and both macros run perfectly. However, if the file is moved into a location that is not on the shared network drive (e.g. a local folder on the PC) then whilst the first macro runs perfectly, the second macro returns:
Debug shows the source of the error to be this line:
The code for both macros is basically identical (I think). The source file hasn't changed other than moving location. The files that are being opened are the same as well - so I know they work in the original location (and i've tested multiple versions of the file that's failing). I say this because I've read elsewhere that the error could be caused by file corruption, so I'm hoping that by proving they work this is not the case.
This is the code snippet from Macro 1 (assume any missing variables are present):
This is the code from Macro 2:
It may be that I can't see the wood for the trees and there is a difference between my two macros, but I really can't see it so I'm hoping someone might be able to explain why one macro fails when the other succeeds - despite working on the network drive.
I'm beating my head against a brick wall on this one. I've got two macros that each open a (different) file and import data into the source workbook. The source workbook was developed on a shared network drive, and both macros run perfectly. However, if the file is moved into a location that is not on the shared network drive (e.g. a local folder on the PC) then whilst the first macro runs perfectly, the second macro returns:
Run-time error '1004' Method 'Open' of object 'Workbooks' failed.
Debug shows the source of the error to be this line:
Code:
Workbooks.Open Filename:=FullResourceFilename, ReadOnly:=False
The code for both macros is basically identical (I think). The source file hasn't changed other than moving location. The files that are being opened are the same as well - so I know they work in the original location (and i've tested multiple versions of the file that's failing). I say this because I've read elsewhere that the error could be caused by file corruption, so I'm hoping that by proving they work this is not the case.
This is the code snippet from Macro 1 (assume any missing variables are present):
Code:
'Set up the variables we're going to be using
Dim NumRow As Long
Dim NumCol As Long
Dim wbSource As Workbook
Dim wbDest As Workbook
Dim LastRow As Long
Dim TableRows As Long
Dim TableCols As Long
Dim HeaderRow As Range
Dim i As Integer
Dim j As Integer
Dim Path As String
Dim Source_Filename As String
Dim FullFilename As String
' Identify the path and name of where the timesheet data is stored
Path = ActiveWorkbook.Path
'MsgBox ("Path = " & Path)
Source_Filename = "Timesheets.xlsx"
'MsgBox ("Source Filename= " & Source_Filename)
FullFilename = Path & "\" & Source_Filename
'MsgBox ("Full path = " & FullFilename)
Application.ScreenUpdating = False
' Activate the master sheet
Set wbDest = ActiveWorkbook
' Open and activate the source data
If Dir(FullFilename) = "" Then
MsgBox "Error - You need to put a file called Timesheets.xlsx in the same folder as this one"
Exit Sub
Else
Workbooks.Open Filename:=FullFilename, ReadOnly:=False
End If
Set wbSource = ActiveWorkbook
This is the code from Macro 2:
Code:
Dim Resource_Path As String ' The path of the resource forecast file
Dim Resource_Filename As String ' The name of the resource forecast file
Dim FullResourceFilename As String ' The full name & path of the resource forecast file
Dim wbSource As Workbook ' Variable to hold the workbook that all the source data comes from
Dim wbDest As Workbook ' Variable to hold the workbook that is the destination for the data
Dim i As Integer ' A counting variable
' Identify the path and name of the Resource Forecast file
Resource_Path = ActiveWorkbook.Path
'MsgBox ("Path = " & Resource_Path)
Resource_Filename = "Resource Sheet*.xlsx"
'MsgBox ("Source Filename = " & Resource_Filename)
FullResourceFilename = Resource_Path & "\" & Resource_Filename
'MsgBox ("Full path = " & FullResourceFilename)
Application.ScreenUpdating = False
Worksheets("Summary").Activate
' Activate the master sheet
Set wbDest = ActiveWorkbook
' Open and activate the source data
If Dir(FullResourceFilename) = "" Then
MsgBox "Error - You need to put your latest Resource Forecast file in the same folder as this one"
Exit Sub
Else
Workbooks.Open Filename:=FullResourceFilename, ReadOnly:=False
End If
Set wbSource = ActiveWorkbook
It may be that I can't see the wood for the trees and there is a difference between my two macros, but I really can't see it so I'm hoping someone might be able to explain why one macro fails when the other succeeds - despite working on the network drive.