Sub or Function not defined

winstela

New Member
Joined
Feb 24, 2019
Messages
28
Office Version
  1. 2016
Platform
  1. 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
Screenshot 2022-04-29 163127.png

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
 

Attachments

  • Screenshot 2022-04-29 162743.png
    Screenshot 2022-04-29 162743.png
    10.8 KB · Views: 2
You're welcome. Now you know what the error message means - for next time. :)
Sometimes it's just a misspelling, but that can cause it also.
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,215,032
Messages
6,122,772
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top