Run Time 1004 - Cant locate the file to open

Razor_Rob

Board Regular
Joined
Aug 18, 2022
Messages
65
Office Version
  1. 365
Platform
  1. Windows
Hi

Ive got a VBA code that opens a file where the user chooses a file

VBA Code:
 Dim fileNameAndPath As Variant
    fileNameAndPath = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLSX), *.XLSX", Title:="Select File To Be Opened")
    If fileNameAndPath = False Then Exit Sub
    'Workbooks.Open Filename:=fileNameAndPath
    Set wbCopy = Workbooks.Open(fileName:=fileNameAndPath, ReadOnly:=True)
    Set wsCopy = wbCopy.Worksheets(1)

and another code to open another workbook on a set path
VBA Code:
Dim ACpath As String
    Dim ACname As String
    Dim fullACname As String
    Dim fExt As String
            
    ACpath = "\\foldername\different folder\Macro\"
    ACname = "Availability Codes"
    fExt = "xlsx"
    fullACname = Dir(ACpath & ACname & "*.*.*." & fExt)
    Set wbAvailabilityCodes = Workbooks.Open(fullACname)    
    Set wsAvailabilityCodes = wbAvailabilityCodes.Worksheets(1)


Now it gives an Error 1004 stating that the file is not there. If the user selects a file in the same folder as the set path the code works fine. It only errors if the user selects a different path for the 1st code.

The error highlights this code
VBA Code:
Set wbAvailabilityCodes = Workbooks.Open(fullACname)
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try inserting a ChDir statement before calling the Dir function:
VBA Code:
...
ChDir ACpath
...
fullACname = Dir(ACpath & ACname & "*.*.*." & fExt)
...
 
Upvote 0
Solution
Can I add something to this to improve the logic better.

How do I change the code so it looks for the latest file in the folder?
ie
Availability Codes 26.10.22.xlsx
Availability Codes 26.09.22.xlsx
Availability Codes 02.08.22.xlsx
 
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,225
Members
453,025
Latest member
Hannah_Pham93

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top