gw2 gold VBA Macro to Create Folder if it Doesn't |
Post Reply |
Author | |
aV3iU4sJ2f
Senior Member Joined: 13 January 2014 Location: Maine Status: Offline Points: 775 |
Post Options
Thanks(0)
Posted: 04 April 2014 at 11:24 |
VBA Macro to Create Folder if it Doesn't Exist The Problem When attempting to save to a file path in visual basic. guild wars 2 gold The file path will not automatically be created if it doesn't exist. Sub test() ActiveWorkbook. gw2 gold SaveAs ("C:\Test\Test.xls") End Sub We want to save the active workbook in the folder C:\Test, if this folder doesn't exist we will receive the following error: The Solution Check if path exists (If path exists its length will be returned, otherwise zero will be) The End Result Sub test() Dim Path As String Path = "C:\Test" If Len(Dir(Path, vbDirectory)) = 0 Then MkDir (Path) End If ActiveWorkbook.SaveAs (Path "\Test.xls") End Sub An Important Note The MkDir Function will only work if the folder proceeding the last exists For instance if you wanted to save to the path C:\Macro\Test\Test.xls You would have to first test if the path C:\Macro exists and if not create if before proceeding the next folder. Sub test() Dim Path As String Path = "C:\Macro" If Len(Dir(Path, vbDirectory)) = 0 Then MkDir (Path) End If If Len(Dir(Path "/Test", vbDirectory)) = 0 Then MkDir (Path "/Test") End If ActiveWorkbook. guild wars 2 gold SaveAs (Path "\Test\Test.xls") End Sub Last updated on August 30, 2012 xboter 2014 |
|
Post Reply | |
Tweet
|
Forum Jump | Forum Permissions You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |