![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Feb 2002
Location: England
Posts: 212
|
In a macro that I have written, I create a new directory. Is it possible to first of all check that a directory with the same name already exists, if it does, not to bother to create the directory and if it does not go ahead and create the directory.
Hope someone can help Thanks Matt |
|
|
|
|
|
#2 |
|
Board Regular
Join Date: Feb 2002
Location: Calgary Canada
Posts: 222
|
try this code :
'''''''''''''''''''''''''' Function Test(YourDirectory As String) As Boolean Test = False TestName = Dir(TestPath, vbDirectory) Do While TestName <> "" If UCase$(YourDirectory) = UCase$(TestName) Then Test = True: Exit Do TestName = Dir ' Get next directory Loop End Function '''''''''''''''''''''''''''''''''''' where TestPath = "c:" or your path and YourDirectory is your folder for test. Test is true if your folder already exists. Good luck! |
|
|
|
|
|
#3 |
|
Board Regular
Join Date: Feb 2002
Location: England
Posts: 212
|
Thanks for the code but I think I am being dumb as I cannot get it to run. I have tried to substitute the path and directory names as suggested, please could you resupply code if my path is C:/ and folder name is Matt.
Many thanks Matt |
|
|
|
|
|
#4 |
|
Board Regular
Join Date: Feb 2002
Location: Calgary Canada
Posts: 222
|
Hello matt,
sorry for my delay, I had a class. copy these code in a module: '''''''''''''''''''''''''''''''''''' Function Test_Folder(YourDirectory, TestPath) As Boolean Test_Folder = False GetFolder = Dir(TestPath, vbDirectory) Do While GetFolder <> "" If UCase$(YourDirectory) = UCase$(GetFolder) Then Test_Folder = True: Exit Do GetFolder = Dir ' Get next directory Loop End Function Sub Main_Test() ''' enter your path here TestPath = "c:" '''please enter your folder name YourDirectory = "Matt" If YourDirectory = "" Then Exit Sub If Test_Folder(YourDirectory, TestPath) = True Then MsgBox (YourDirectory & " already exists.") Else MsgBox (YourDirectory & " does not exist.") End If End Sub '''''''''''''''''''''''''''''''''' now run Main_Test. Good luck! Hamid |
|
|
|
|
|
#5 |
|
Board Regular
Join Date: Feb 2002
Location: England
Posts: 212
|
Hamid
That's worked, thanks for your help and patience |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|