Vba Create Folder And Subfolders

[Solved] Vba Create Folder And Subfolders | Vb - Code Explorer | yomemimo.com
Question : vba create folder and subfolders

Answered by : anxious-alligator-61ke0kgw6vaj

If Dir(YourPath, vbDirectory) = "" Then Shell ("cmd /c mkdir """ & YourPath & """")
End If
'@waternova I got around this by using WScript object: Set wsh = CreateObject("WScript.Shell") wsh.Run "cmd /c mkdir """ & YourPath & """", 0, True 'This will wait until the cmd is finished – 

Source : https://stackoverflow.com/questions/10803834/create-a-folder-and-sub-folder-in-excel-vba | Last Update : Tue, 19 Apr 22

Question : vba create folder and subfolders

Answered by : anxious-alligator-61ke0kgw6vaj

'requires reference to Microsoft Scripting Runtime
Sub MakeFolder()
Dim strComp As String, strPart As String, strPath As String
strComp = Range("A1") ' assumes company name in A1
strPart = CleanName(Range("C1")) ' assumes part in C1
strPath = "C:\Images\"
If Not FolderExists(strPath & strComp) Then
'company doesn't exist, so create full path FolderCreate strPath & strComp & "\" & strPart
Else
'company does exist, but does part folder If Not FolderExists(strPath & strComp & "\" & strPart) Then FolderCreate strPath & strComp & "\" & strPart End If
End If
End Sub
Function FolderCreate(ByVal path As String) As Boolean
FolderCreate = True
Dim fso As New FileSystemObject
If Functions.FolderExists(path) Then Exit Function
Else On Error GoTo DeadInTheWater fso.CreateFolder path ' could there be any error with this, like if the path is really screwed up? Exit Function
End If
DeadInTheWater: MsgBox "A folder could not be created for the following path: " & path & ". Check the path name and try again." FolderCreate = False Exit Function
End Function
Function FolderExists(ByVal path As String) As Boolean
FolderExists = False
Dim fso As New FileSystemObject
If fso.FolderExists(path) Then FolderExists = True
End Function
Function CleanName(strName as String) as String
'will clean part # name so it can be made into valid folder name
'may need to add more lines to get rid of other characters CleanName = Replace(strName, "/","") CleanName = Replace(CleanName, "*","") etc...
End Function

Source : https://stackoverflow.com/questions/10803834/create-a-folder-and-sub-folder-in-excel-vba | Last Update : Tue, 19 Apr 22

Answers related to vba create folder and subfolders

Code Explorer Popular Question For Vb