Excel 2013 VBA - How to create a folder using VBA, how to save a file in new folder?

1.  Using Excel VBA, how do I create a folder named "john" inside of c:\temp
2.  How do I save my active workbook in that folder c:\temp\john


Thank you.
brothertruffle880Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

abbas abdullaCommented:
Hi,
try this
Sub MakeFolderAndSaveActiveWorkbook()
Application.DisplayAlerts = False
Dim folderpath As String
folderpath = "c:\temp\john\"
MkDir folderpath
ActiveWorkbook.SaveAs folderpath & ActiveWorkbook.Name & ".xlsx"
Application.DisplayAlerts = True
End Sub

Open in new window

0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
It's better to check if the directory doesn't already exist and then create a new one.
Sub CreateFolderAndSaveFile()
Dim folderPath As String
Dim fileName As String

fileName = ActiveWorkbook.Name
folderPath = "C:\Temp\John"
If Dir(folderPath, vbDirectory) = vbNullString Then
    MkDir folderPath
End If
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs folderPath & "\" & fileName
End Sub

Open in new window

1
Fabrice LambertFabrice LambertCommented:
Hi,

I do not recommend using the Dir() function as it store its search criteria in a global scope, thus is unreliable.
What if your new search criterias overwrite an old one ?
What if you call a function that overwrite your search criterias ?
Bad surprise in the corner ...

I also do not recommend using the Activeworkbook object as it is a major source of troubles.

Better use the FSO object, wich is also more explicit:
Sub CreateFolderAndSaveFile()
    Dim fso As Object       '// Scripting.FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    Const folderPath As String = "C:\Temp\John"
    If (Not fso.FolderExists(folderPath)) Then
        fso.CreateFolder folderPath
    End If
    ThisWorkbook.SaveAs folderPath & "\" & ThisWorkbook.Name
    Set fso = Nothing
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.