Solved

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

Posted on 2013-06-10
5
3,534 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 51

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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

695 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