我制作了一张Excel工作表,将数据处理到工作表并将其另存为某个文件夹中的新工作簿 - 子文件夹(命名为文件名的第一部分)。
代码工作正常,但如果所需路径不存在,我想制作一个新文件夹。绝对可以通过' If'来实现。功能,但我不知道如何创建新文件夹。
注意:跳过下面代码中的某些部分,为了保持简短,我只是提到了值得一提的部分。
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
答案 0 :(得分:3)
你走了:
If Dir(sMainFolder & "\"& sSubFolder & "\", 16) <> vbNullString Then
Else
MkDir (sMainFolder & "\"& sSubFolder & "\")
End If