Create Folder in VBA

bamaisgreat

Well-known Member
Joined
Jan 23, 2012
Messages
831
Office Version
  1. 365
Platform
  1. Windows
Thanks for looking at my post.

What I would like to do is have a macro in a workbook that when run it would create a folder with the same name as the excel file in the same location. Then I would like it to somehow save the excel file in the folder that was just created and delete or kill the old one.

Make since??
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi,
Try this:
Rich (BB code):
Sub Test()
  Dim i As Long, Path As String, NewPath As String, FileName As String, WbName As String
  Path = ThisWorkbook.Path
  FileName = ThisWorkbook.Name
  WbName = Left(FileName, InStrRev(FileName, ".") - 1)
  If UCase(Path) Like "*" & UCase(WbName) Then Exit Sub
  NewPath = Path & "\" & WbName
  MkDir NewPath
  ThisWorkbook.SaveAs NewPath & "\" & ThisWorkbook.Name
  Kill Path & "\" & FileName
End Sub
Regards
 
Upvote 0
.
Similar ...

Code:
Option Explicit


Sub CommandButton1_Click()
    Dim fso As Object
    Dim Filename As String
    Dim fldrpath As String
    Set fso = CreateObject("scripting.filesystemobject")
    Filename = ActiveWorkbook.Name
    If InStr(Filename, ".") > 0 Then
       Filename = Left(Filename, InStr(Filename, ".") - 1)
    End If
    fldrpath = Application.ActiveWorkbook.Path & "\" & Filename
    If Not fso.folderexists(fldrpath) Then
        fso.createfolder (fldrpath)
    End If
    ThisWorkbook.SaveCopyAs fldrpath & "\" & Filename & ".xlsm"
    With ThisWorkbook
        .Saved = True
        .ChangeFileAccess xlReadOnly
        Kill .FullName
        Application.Quit
    End With
End Sub
 
Upvote 0
Works Great ! Could it be edited so I could put it in my personal workbook?
 
Upvote 0
Could it be edited so I could put it in my personal workbook?
Sure, just replace ThisWorkbook by ActiveWorkbook:
Rich (BB code):
Sub Test()
  Dim i As Long, Path As String, NewPath As String, FileName As String, WbName As String
  Path = ActiveWorkbook.Path
  FileName = ActiveWorkbook.Name
  WbName = Left(FileName, InStrRev(FileName, ".") - 1)
  If UCase(Path) Like "*" & UCase(WbName) Then Exit Sub
  NewPath = Path & "\" & WbName
  MkDir NewPath
  ActiveWorkbook.SaveAs NewPath & "\" & ActiveWorkbook.Name
  Kill Path & "\" & FileName
End Sub
The same is for the code of Logit
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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