Error On Opening File

HiSan

New Member
Joined
Mar 13, 2019
Messages
5
Hi all, I got Run-time error "Sorry, we couldn't find Inventory_SWK - 080319.xlsx. Is it possible it was moved, renamed or deleted?"

Below is my code:

Dim sFileName As String
Dim lRow As Long
Dim wb As Workbook


sFileName = Dir(ThisWorkbook.Path & "\Inventory_SWK*")
Workbooks.Open (sFileName)

This macro is saved together with the file I want to open which is Inventory_SWK - 080319.xlsx. Not sure why macro found this file but says couldn't find in the error message and refuse to open it.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hello!

Not sure if this will work but try ThisWorkbook.FullName instead of ThisWorkbook.Path - then remove the & "\Inventory_SWK*" part as .FullName provides that bit as well.

Let me know if that works!
 
Upvote 0
Just looked into it more, ignore my previous answer as it's incorrect.

Try this:

Code:
Sub path()
    Dim sFileName As String
    
    sFileName = ThisWorkbook.path & "\Inventory_SWK*"
    Workbooks.Open (sFileName)
End Sub
 
Upvote 0
Welcome to the forum.

You need to include the path when opening the workbook:


Code:
Workbooks.Open ThisWorkbook.Path & "\" & sFileName
 
Upvote 0
Welcome to the forum.

You need to include the path when opening the workbook:


Code:
Workbooks.Open ThisWorkbook.Path & "\" & sFileName

Hi RoryA, It works! Wonder if I have included the path in sFileName, why have to include again in opening workbook?
 
Upvote 0
You didn't include the path. You assigned the result of Dir, which is just the file name, without any path.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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