0
Sub Test_Run()

MkDir "C:\ST\temp\AM"

End Sub

I am trying to create a new folder (AM) in an already existing directory which is C:\ST\temp. But it's causing error showing message: Argument not optional

Scott Craner
  • 148,073
  • 10
  • 49
  • 81
  • 2
    Put the caret on the word `MkDir` and press Shift+F2. Where does that bring you? – GSerg Nov 14 '22 at 23:31
  • After putting caret on MkDir and pressing shift+F2 key, it got redirected to a different module query in same workbook. That query code is as follows: 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 FSO.CreateFolder path End If End Function – vibhor tiwari Nov 15 '22 at 10:35
  • So you have defined your own `MkDir` function (as opposed to the [built-in `MkDir` statement](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/mkdir-statement)) that accepts two parameters. You are supplying one. – GSerg Nov 15 '22 at 13:18
  • That makes sense, it was actually a part of complicated created by someone else, so didn't notice that they already defined it as another function. Thanks alot for that :) – vibhor tiwari Nov 15 '22 at 15:52

1 Answers1

0

Your MkDir function is expecting two arguments and you are only sending one.

Instead of calling

MkDir "C:\ST\temp\AM"

Change it to:

MkDir "C:\ST\temp", "AM"

and it should work.

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 
      FSO.CreateFolder path 
   End If 
End Function
SixSigmaGuy
  • 313
  • 3
  • 11