RandomUserCode
New Member
- Joined
- Aug 4, 2021
- Messages
- 26
- Office Version
- 365
- Platform
- Windows
- MacOS
Hi everyone. I want to make some error handling when the file in the given path, doesn't exist. Currently the if-statement doesn't work as when the two files i want to import is in the path, it goes to the else-statement and debug print "error". So i dont know why my two booleans are "false" when the files exists in the path. I have tried to say Dir(Len(filePath1) = "" then exit sub, but that fails too. Hope someone can help me out with this error handling
Code below:
Code below:
VBA Code:
Dim filePath1 As String
Dim filePath2 As String
Dim fileExists1 As Boolean
Dim fileExists2 As Boolean
filePath1 = "my path" & Format(Date, "ddmmyyyy")
filePath2 = "my path" & Format(Date, "ddmmyyyy")
fileExists1 = Dir(filePath_Counterparty) <> ""
fileExists2 = Dir(filePath_Counterparty) <> ""
Debug.Print fileExists1
Debug.Print fileExists2
If fileExists1 = False Or fileExists2 = False Then
Debug.Print "Error"
Exit Sub
Else
Debug.Print "Success"
Set closedBook = Workbooks.Open(filePath1 & ".csv")
closedBook.Sheets("sheet1" & Format(Date, "ddmmyyyy")).Copy After:=ThisWorkbook.Sheets(1)
closedBook.Close SaveChanges:=False
Set closedBook = Workbooks.Open(filePath2 & ".csv")
closedBook.Sheets("sheet2" & Format(Date, "ddmmyyyy")).Copy After:=ThisWorkbook.Sheets(2)
closedBook.Close SaveChanges:=False
End If