如果path不存在,则创build新文件夹(否则粘贴到现有文件夹中)

我制作了一个Excel工作表,它将数据处理到工作表并将其另存为某个文件夹 – 子文件夹(名称类似于文件名的第一部分)中的新工作簿。

代码工作正常,但如果所需的path不存在,我想创build一个新的文件夹。 应该肯定有可能实现一个“如果”function,但我不知道如何创build新的文件夹。

注意:跳过下面的代码中的一部分,以保持它的简短,我只能通过值得一提的部分。

Sub SaveSheetAs() Dim sMainFolder as String Dim sFileName as string Dim sSubFolder as string sMainFolder = Z:\Parts Manufacturing\5. Kwaliteit\130 - in proces meten\EindProject\Bron '(Main folder, which isn't variable) sFileName = 4022 646 68954#1234 '(Part name with Unique number)'variable number, in de real code this number is received by refering to a range("") sSubFolder = 4022 646 68954 '(variable number, in de real code this number is received by refering to a range("") ActiveWorkbook.SaveAs Filename:=sMainFolder & "\"& sSubFolder & "\" & sFileName & ".csv", FileFormat:=xlCSV, CreateBackup:=False, Local:=True end sub 

干得好 :

 If Dir(sMainFolder & "\"& sSubFolder & "\", 16) <> vbNullString Then Else MkDir (sMainFolder & "\"& sSubFolder & "\") End If