Saving file automatically VBA

Asked

Viewed 66 times

2

Guys I made this macro to create folders and subfolders automatically, but I need to save the file I’m editing inside the created folder... someone can help me?

Follows the script

' MACRO PARA CRIAR PASTA DE ANO/MES/DIA

Public Const sCaminho = ""
Dim Pasta As New FileSystemObject

Public Function fnccriardiretorio(data As Date)
If Pasta.FolderExists(sCaminho & "\" & Format(data, "yyyy")) Then
    fncmes (data)
Else
    Pasta.CreateFolder (sCaminho & "\" & Format(data, "yyyy"))
    fncmes (data)
End If


End Function

Public Function fncmes(data As Date)
If Pasta.FolderExists(sCaminho & "\" & Format(data, "yyyy") & _
"\" & Format(data, "mmmm")) Then
    Call fncdia(data)
Else
    Pasta.CreateFolder (sCaminho & "\" & Format(data, "yyyy") & _
"\" & Format(data, "mmmm"))
    Call fncdia(data)
End If
End Function

Public Function fncdia(data As Date)
If Pasta.FolderExists(sCaminho & "\" & Format(data, "yyyy") & _
"\" & Format(data, "mmmm") & "\" & Format(data, "dd")) Then

Else
    Pasta.CreateFolder (sCaminho & "\" & Format(data, "yyyy") & _
"\" & Format(data, "mmmm")) & "\" & Format(data, "dd")
End If


End Function
Sub chamafuncao()
Dim data As Date
data = InputBox("Entre com a Data _EX DD/MM/AAAA")
Call fnccriardiretorio(data)
End Sub

1 answer

2


To save the file itself in xlsm format in the folder you created, you can use the Workbook Saveas method.

fname = (sCaminho & "\" & Format(data, "yyyy") & _
"\" & Format(data, "mmmm")) & "\" & Format(data, "dd") & _
"\" & "nomedoarquivo"

Workbook.SaveAs FileName:=fname, FileFormat:=xlOpenXMLWorkbookMacroEnabled
  • this command would be inside the Sub, right?

  • Yes, I would stay in the job chamafuncao(), after the creation of the directory.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.