winstela
New Member
- Joined
- Feb 24, 2019
- Messages
- 28
- Office Version
- 2016
- Platform
- Windows
Hello
I am having problems trying to create folders within folders.
I have code that will open a workbook if it exists and if not it will open another workbook which will be a blank template of the first workbook.
But if the path does not exist I want the macro to use the blank template and then create the path and folders. my current code which is not working is below. The sub or function not defined highlights CreateFolder
If you can help much appreciated.
Thanks
I am having problems trying to create folders within folders.
I have code that will open a workbook if it exists and if not it will open another workbook which will be a blank template of the first workbook.
But if the path does not exist I want the macro to use the blank template and then create the path and folders. my current code which is not working is below. The sub or function not defined highlights CreateFolder
VBA Code:
Sub openmyfile()
Dim Path As String, File As String, wb As Workbook
Path = Range("B2")
File = Range("B3")
'If File exists then open.
If Dir(Path & File & ".xlsm") <> "" Then
Set wb = Workbooks.Open(Path & File & ".xlsm")
Else 'else, open the other one:
Set wb = Workbooks.Open(Path & "05 Daily Bowler - Systems - May 2022.xlsm")
End If
Stop 'check if the workbook has been open and press F5 to let code finishing
If wb = Len(Dir(Path)) = 0 Then
strDir = Range("j2")
strPath = Range("J3") & Range("j4") & Range("j5")
CreateFolder Path
'wb.Close SaveChanges:=True
End Sub
'requires reference to Microsoft Scripting Runtime
Function MkDir(strDir As String, strPath As String)
Dim fso As New FileSystemObject
Dim Path As String
Path = strPath & strDir
If Not fso.FolderExists(Path) Then
' doesn't exist, so create the folder
fso.CreateFolder Path
End If
End Function
If you can help much appreciated.
Thanks