1

I've made an Excel sheet which processes data to a Sheet and Saves it as a new workbook in a certain Folder - Subfolder (named like the first part of the file names).

The code works fine but I'd like to make a new folder if the required path does not exists. Should definitely be possible to achieve with an 'If' function, but I don't know how to create new folders.

Note: skipped some part in the code below, to keep it short I only past the parts worth mentioning.

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

1 Answer 1

3

Here you go :

If Dir(sMainFolder & "\"& sSubFolder & "\", 16) <> vbNullString Then
Else
    MkDir (sMainFolder & "\"& sSubFolder & "\")
End If
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I'm going to try it, and I'' google what the MkDir function does. something like "make direction/directory" i gues
MkDir will create the folder that is missing in your path, so if you have to create multiple folders, you'll have to do it step by step (or at least that's what I did last time I used it!)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.