Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA Check if directory exists error

I have a spreadsheet that upon clicking a button will duplicate itself by copying/pasting everything to a new workbook and save the file with a name that is dependent upon some variable values (taken from cells on the spreadsheet). My current goal is to get it to save the sheet in different folders depending on the name of client name (cell value held in variable), while this works on the first run, I get an error after.

The code checks if the directory exists and creates it if not. This works, but after it is created, running it a second time throws the error:

Runtime Error 75 - path/file access error.

My code:

Sub Pastefile()

Dim client As String
Dim site As String
Dim screeningdate As Date
screeningdate = Range("b7").Value
Dim screeningdate_text As String
screeningdate_text = Format$(screeningdate, "yyyy\-mm\-dd")
client = Range("B3").Value
site = Range("B23").Value

Dim SrceFile
Dim DestFile

If Dir("C:\2013 Recieved Schedules" & "\" & client) = Empty Then
    MkDir "C:\2013 Recieved Schedules" & "\" & client
End If

SrceFile = "C:\2013 Recieved Schedules\schedule template.xlsx"
DestFile = "C:\2013 Recieved Schedules\" & client & "\" & client & " " & site & " " & screeningdate_text & ".xlsx"

FileCopy SrceFile, DestFile

Range("A1:I37").Select
Selection.Copy
Workbooks.Open Filename:= _
    "C:\2013 Recieved Schedules\" & client & "\" & client & " " & site & " " & screeningdate_text & ".xlsx", UpdateLinks:= _
    0
Range("A1:I37").PasteSpecial Paste:=xlPasteValues
Range("C6").Select
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWindow.Close

End Sub

You'll have to excuse my lack of knowledge in this area, I am still learning. I have a very strong feeling it has something to do with the directory checking logic, as when the error is thrown the MkDir line is highlighted.

like image 812
user1571463 Avatar asked Mar 18 '13 15:03

user1571463


People also ask

How do you check if a folder exists in VBA Excel?

We will use the DIR function to check if a file exists. If the file doesn't exist, we will display a “File does not exist” message to the user. If the file exists, we will open the file. We will use the DIR function to check if a folder exists.

What is vbDirectory VBA?

VBA DIR function is also known as the directory function, this is an inbuilt function in VBA which is used to give us the file name of a given file or a folder but we need to provide the path for the file, the output returned by this function is string as it returns the name of the file, there are two arguments to this ...

What is vbDirectory?

vbDirectory. 16. Specifies directories or folders in addition to files with no attributes. vbAlias.

How do you check if a file exists in Excel?

You can run the macro by select Macro > Macros under the Tools menu. Then select the macro called CheckIfFileExists and click on the Run button. After you run the macro, the values in column B should indicate whether the files exist or not. You can press Alt + F11 to view the VBA code.


3 Answers

To check for the existence of a directory using Dir, you need to specify vbDirectory as the second argument, as in something like:

If Dir("C:\2013 Recieved Schedules" & "\" & client, vbDirectory) = "" Then

Note that, with vbDirectory, Dir will return a non-empty string if the specified path already exists as a directory or as a file (provided the file doesn't have any of the read-only, hidden, or system attributes). You could use GetAttr to be certain it's a directory and not a file.

like image 128
Brian Camire Avatar answered Sep 24 '22 04:09

Brian Camire


Use the FolderExists method of the Scripting object.

Public Function dirExists(s_directory As String) As Boolean
    Dim oFSO As Object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    dirExists = oFSO.FolderExists(s_directory)
End Function
like image 40
ozmike Avatar answered Sep 26 '22 04:09

ozmike


To be certain that a folder exists (and not a file) I use this function:

Public Function FolderExists(strFolderPath As String) As Boolean
    On Error Resume Next
    FolderExists = ((GetAttr(strFolderPath) And vbDirectory) = vbDirectory)
    On Error GoTo 0
End Function

It works both, with \ at the end and without.

like image 7
ZygD Avatar answered Sep 24 '22 04:09

ZygD