Solved

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

Posted on 2013-06-10
5
3,124 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 49

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you how to use shortcut menus in the Access run-time environment.
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

773 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