Solved

VBA code to save a file in folder that changes each month and year

Posted on 2013-06-10
5
2,919 Views
Last Modified: 2013-06-11
I need some code that will save a file to a folder that changes every month and Year.

i.e.

folder path = G:\rights\Mkt\Break\2013\06 Jun

Next month it will be

folder path = G:\rights\Mkt\Break\2013\06 Jul

and next year

folder path = G:\rights\Mkt\Break\2014\06 Jun

Appreciate help provideing VBA code. I am using 2010

Thanks
0
Comment
Question by:Jagwarman
  • 3
5 Comments
 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 39234548
Hi,

What is 06 for?

if not relevant

 folderPath = "G:\rights\Mkt\Break\" & Year(Date) & "\06 " & Format(Date, "Mmm")

Open in new window


Regards
0
 

Author Comment

by:Jagwarman
ID: 39234560
oops it should have read:

Next month it will be

folder path = G:\rights\Mkt\Break\2013\07 Jul
0
 

Author Comment

by:Jagwarman
ID: 39234583
Below is the code I have used on other files but for some reason this fails with "Method 'Save As' of object'_Workbook' failed.....

Sub CreateFile()
Dim strFile As String
strFile = Date_FileName("\\G:\rights\Mkt\Break\2013\06 Jun\", "COB ")

If Dir(strFile) <> "" Then
   If MsgBox("File already exists - overwrite?", vbYesNo) = vbYes Then
      Kill strFile
     

   Else
      Exit Sub
   End If
End If


ActiveWorkbook.SaveAs Filename:= _
strFile, _
FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False


'MsgBox "File Created, close file"


End Sub

Function Date_FileName(pPath As String, pFilePrefix As String) As String
Dim DayOfWeek As Integer, DayDiff As Integer, CharDate As String, MonthNo As Integer, CharYear As String, CharMonth  As String
Dim Result As String
   
    DayOfWeek = Weekday(Date)                                   ' Sunday is 1, Monday is 2, Tuesday is 3 etc
    If DayOfWeek = 2 Then                                       ' If a monday then we need the date as of friday, otherwise previous day
        DayDiff = 3
    Else
        If DayOfWeek = 1 Then
          DayDiff = 2                                           ' If a Sunday then we need the date as of friday, otherwise previous day
        Else
          DayDiff = 1                                           ' otherwise it should be set to the previous day
        End If
    End If
    CharDate = Format(Date - DayDiff, "ddmmyyyy")               ' Get the character date formwatted how we want it

    CharMonth = Format(Date - DayDiff, "m mmm yyyy")
    Result = pPath & CharMonth & "\" & pFilePrefix & "" & CharDate & ".xls"
    Date_FileName = Result
End Function
0
 
LVL 6

Accepted Solution

by:
Michael earned 500 total points
ID: 39234654
Or try this code, which also checks if a folder already exists, if not it creates it.
It assumes you want to save the current workbook, and that you want to save the file in the folder of the current month in the current year.
If not, you can replace now with the right date with "" around it.

Sub monthFolder()
    
    Dim sBasePath As String
    Dim sFilename As String
    Dim sMonth As String
    Dim sPath As String
    Dim sYear As String
    
    sFilename = "myWorkbook.xlsm" 'Set filename
    sBasePath = "C:\rights\Mkt\Break\" 'Set base path
    If Right(sBasePath, 1) <> "\" Then sBasePath = sBasePath & "\" 'Check for "\"
    
    sYear = Year(Now) 'Set this year
    sPath = sBasePath & sYear & "\" 'Set path incl. year
    If Dir(sPath, vbDirectory) = "" Then
        MkDir sPath 'Create directory if it does not exist
    End If
    
    If Len(Month(Now)) = 1 Then
        sMonth = "0" & Month(Now) 'Add leading zero
    Else
        sMonth = Month(Now)
    End If
    
    sMonth = sMonth & " " & MonthName(Month(Now), True) 'Set month as number + name
    sPath = sPath & sMonth & "\" 'Set path incl. month
    
    If Dir(sPath, vbDirectory) = "" Then
        MkDir sPath 'Create directory if it does not exist
    End If
    
    ActiveWorkbook.SaveAs sBasePath & sFilename
    
End Sub

Open in new window

0
 

Author Closing Comment

by:Jagwarman
ID: 39236907
jazzyjoop, I will accept your solution as it works [although not for what I wanted this time, I will keep it on file] I sorted out my original piece of code and it now works.

thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This collection of functions covers all the normal rounding methods of just about any numeric value.
My experience with Windows 10 over a one year period and suggestions for smooth operation
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now