Help with finding WorkBook from folder

Yachrishere

New Member
Joined
May 6, 2019
Messages
6
Hi. I am having a problem with a macro looking for a file with a variable name.
This is the code.
[vba]
Dim MyFile As String
Dim strFilePath As String
strFilePath = Range("E11").Value & "Quoting Files"
MyFile = Dir("Foundation*")
On Error GoTo Error_handler:
' all good so far
If CheckFileIsOpen("Foundations*") = True Then ' if there is a file open then it does what it should
MsgBox "File was already Open"
Workbooks("Foundations*").Activate
Application.GoTo Range("A1"), True
ActiveWindow.Zoom = 84
ActiveWindow.WindowState = xlMaximized


Else
Workbooks.Open Filename:=(sPath & "\Quote" & "\Quote Sheets" & MyFile) ' this is working as "sPath" is defined earlier
Application.GoTo Range("A1"), True
ActiveWindow.Zoom = 84
ActiveWindow.WindowState = xlMaximized
Exit Sub
Error_handler:
' now this is the problem part that gives me a runtime error 1004
Workbooks.Open Filename:=strFilePath & MyFile
Application.GoTo Range("A1"), True
ActiveWindow.Zoom = 84
ActiveWindow.WindowState = xlMaximized
MsgBox ("This is a Template File as the file does'nt appear in the folder" _
& vbNewLine & "You will need to save this to the Clients Folder")

End If
Unload UForm1
[vba]

the problem is with this line - Workbooks.Open Filename:=strFilePath & MyFile -
a runtime error 1004 says it can't find the folder but it does exist.

Any advice???
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Here is a method to use to see what is going on
- the string in the message box should clarify things

Place this line above Workbooks.Open Filename:=strFilePath & MyFile
Code:
If Dir(strFilePath & myFile) = "" Then MsgBox UCase(strFilePath) & LCase(myFile), vbCritical, "FilePath NOT FOUND"

The usual cuplrit (which appears to be the case here) is a missing path separator
- likely fix required is
Code:
strFilePath = Range("E11").Value & "Quoting Files[SIZE=3][COLOR=#ff0000]\[/COLOR][/SIZE]"

another way
Code:
Workbooks.Open Filename:=strFilePath & "[SIZE=3][COLOR=#ff0000]\[/COLOR][/SIZE]" & MyFile
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
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