Check if file exists. Keeps skipping the existing file.

Wicked_

Board Regular
Joined
Jun 5, 2018
Messages
81
Hi.

So, i got a code for checking if a file exists from google.
The problem is with it, even tho the file actually exists, it keep skipping it, as it didn't exists.

Code:

Sub CFF()
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")


If fso.FileExists("Desktop\SkjKa.xlsm") Then
' do what you like in the case where the file exists
UserForm3.Show

Else
' do whatever you wanted to do where the file doesn't exist
Welcome.Show


End If

End Sub

If the file dont exist, it runs the welcome.show.
However, if the file exists in desktop, it still runs welcome.show, and not userform 3.

Does anyone know whats wrong here?

Thanks
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
You must put the complete path:

eg.

"C:\Users\damor\Desktop\SkjKa.xlsm"
 
Upvote 0
Aha. That actually worked.

How can i combine this with Environ?
This is going to be on multiple machines / users. So instead of C:\Users\damor\Desktop\SkjKa.xlsm, can i do something like C:\Users\Environ$("USERPROFILE")\Desktop\SkjKa.xlsm ?
 
Upvote 0
Try:

Code:
    Dim wDesk As String, wFile As String
    On Error Resume Next
    wDesk = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    wFile = wDesk & "\" & "SkjKa.xlsm"
 
Upvote 0
That was almost perfect :)

The only "issue" i had, when opening SkjKa, was when i closed the UserForm 3, the "welcome" userform popped up again. Like it did both "then" and "else"

Might be my implentation of the code, not sure:

Sub CFF()
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")


Dim wDesk As String, wFile As String
On Error Resume Next
wDesk = CreateObject("WScript.Shell").SpecialFolders("Desktop")
wFile = wDesk & "" & "SkjKa.xlsm"




UserForm3.Show

Welcome.Show


End Sub
 
Upvote 0
Try
Code:
 Environ("USERPROFILE") & "[COLOR=#333333]\Desktop\[/COLOR][COLOR=#333333][COLOR=#333333]SkjKa.xlsm"[/COLOR][/COLOR]

Pefect!. Works like a charm!.

Il try on different computers and users, to be sure as well. But so far, so good :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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