PDA

View Full Version : [SOLVED:] IF Else statement not executing properly



hrq
12-07-2022, 04:42 AM
Hi All, how can the code be amend below so that subfolderpath folder will be created? Currently, if folderpath exist but subfolderpath does not exist, it kind of jump straight to End If.

I tried to include an ElseIf, it still doesn't work.

The code as follows:



If Dir(folderpath, vbDirectory) = "" And Dir(subfolderpath, vbDirectory) = "" Then
'check whether folder exist
MkDir folderpath
'create folder
MkDir subfolderpath
'create folder
If Dir(subfolderpath, vbDirectory) = "" Then
MkDir subfolderpath
'create folder
counter = counter + 1
End If
End If




If Dir(folderpath, vbDirectory) = "" And Dir(subfolderpath, vbDirectory) = "" Then
'check whether folder exist
MkDir folderpath
'create folder
MkDir subfolderpath
'create folder
ElseIf Dir(subfolderpath, vbDirectory) = "" Then
MkDir subfolderpath
'create folder
counter = counter + 1
End If

georgiboy
12-07-2022, 06:35 AM
This is because within the first If statement both of the folders need to not exist before it fires any of the rest of the code. I would imagine you need to break those two parts seperated by the 'And' into their own If's.

So:
If folderpath does not exixst then
'Make both folderpath and subfolderpath
ElseIf subfolderpath does not exist the
'Make only subfolderpath
Else
'Something else

Untested code:

If Dir(folderpath, vbDirectory) = "" Then
MkDir folderpath
MkDir subfolderpath
ElseIf Dir(subfolderpath, vbDirectory) = "" Then
MkDir subfolderpath
Else
'something else
End If

hrq
12-07-2022, 08:10 PM
thanks for help! it works perfectly!