+ Reply to Thread
Results 1 to 5 of 5

Programatically save in Quarter file based on "text-date" in cell

Hybrid View

  1. #1
    Valued Forum Contributor ron2k_1's Avatar
    Join Date
    09-30-2009
    Location
    Not the USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    606

    Programatically save in Quarter file based on "text-date" in cell

    Hi guys,

    I have the following directories in each fiscal year:
    \\server1\Finance\FS\Company 1\2012\Qtr 1\Apr
    \\server1\Finance\FS\Company 1\2012\Qtr 1\May
    \\server1\Finance\FS\Company 1\2012\Qtr 1\Jun

    \\server1\Finance\FS\Company 1\2012\Qtr 2\Jul
    \\server1\Finance\FS\Company 1\2012\Qtr 2\Aug
    \\server1\Finance\FS\Company 1\2012\Qtr 2\Sep

    \\server1\Finance\FS\Company 1\2012\Qtr 3\Oct
    \\server1\Finance\FS\Company 1\2012\Qtr 3\Nov
    \\server1\Finance\FS\Company 1\2012\Qtr 3\Dec

    \\server1\Finance\FS\Company 1\2012\Qtr 4\Jan
    \\server1\Finance\FS\Company 1\2012\Qtr 4\Feb
    \\server1\Finance\FS\Company 1\2012\Qtr 4\Mar

    So I have this file where in A1 I'll have a data in text format YYYYMMDD. And I want to programatically save the file in the respective month in the respect quarter in the respective year. So for example I'd have the following dates and where I'd like the file to be saved:

    20120428 save in \\server1\Finance\FS\Company 1\2012\Qtr 1\Apr
    20120730 save in \\server1\Finance\FS\Company 1\2012\Qtr 2\Jul
    20121231 save in \\server1\Finance\FS\Company 1\2012\Qtr 3\Dec

    20130229 save in \\server1\Finance\FS\Company 1\2012\Qtr 4\Feb
    20130331 save in \\server1\Finance\FS\Company 1\2012\Qtr 4\Mar

    20130530 save in \\server1\Finance\FS\Company 1\2013\Qtr 1\May

    Do you follow? Everything between April 1 2012 and Mar 31 2013 I'd like it saved in folder 2012 in its respective month and respective quarter. You'll notice that although 20130229 and 20130331 have a year date of 2013, but for filing purposes I want them in year 2012 Qtr 4. Then 20130530 will be in Qtr 1 of 2013, and so forth and so forth.

    Any ideers guys?

    Ron
    Ron
    Knowledge is knowing that a tomato and pepper are fruits. Wisdom is knowing whether to put these in a fruit salad

    Kindly

    [1] Use code tags. Place "[code]" before the first line of code and "[/code"]" after the last line of code. Exclude quotation marks
    [2] Mark your post [SOLVED] if it has been answered satisfactorily by editing your original post in advanced mode.
    [3] Thank (using the little scale) those that provided useful help; its nice and its very well appreciated

  2. #2
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Programatically save in Quarter file based on "text-date" in cell

    Well, I'm sure you're much better at programming it, but maybe you could first store the month as a variable(MyMonth), then subtract 3 months from the date and store the year and quarter(MyYear, MyQuarter)? And the folder path would look like "\\server1\Finance\FS\Company 1\" & MyYear & "\" & MyQuarter "\" & MyMonth.

    On a second thought, maybe not subtract 3 months (wouldn't give exact results for all dates), but set up a select case structure for determining each quarter. And when it's Q1, you add one year, when it's Q4, you subtract one year.

    Edit: I'll try to cook some code, sec!

    Edit2: I said some gibberish here but whatever. ;<
    Last edited by RHCPgergo; 10-29-2012 at 08:42 PM.

  3. #3
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Programatically save in Quarter file based on "text-date" in cell

    Not very elegant, but here you go. I rate myself A+! :P
    Sub FinanceFolders()
    
    Dim Mymonth As String, MyYear, MyQtr, MyPath
    Dim MonthArray(1 To 12) As String
    
        MonthArray(1) = "Jan"
        MonthArray(2) = "Feb"
        MonthArray(3) = "Mar"
        MonthArray(4) = "Apr"
        MonthArray(5) = "May"
        MonthArray(6) = "Jun"
        MonthArray(7) = "Jul"
        MonthArray(8) = "Aug"
        MonthArray(9) = "Sep"
        MonthArray(10) = "Oct"
        MonthArray(11) = "Now"
        MonthArray(12) = "Dec"
    
    Mymonth = MonthArray(Mid(Range("A1"), 5, 2))
    
    Select Case Mid(Range("A1"), 5, 2)
        Case 1: MyQtr = "4"
        Case 2: MyQtr = "4"
        Case 3: MyQtr = "4"
        Case 4: MyQtr = "1"
        Case 5: MyQtr = "1"
        Case 6: MyQtr = "1"
        Case 7: MyQtr = "2"
        Case 8: MyQtr = "2"
        Case 9: MyQtr = "2"
        Case 10: MyQtr = "3"
        Case 11: MyQtr = "3"
        Case 12: MyQtr = "3"
    End Select
    
    Select Case MyQtr
        Case 1: MyYear = Left(Range("A1"), 4)
        Case 2: MyYear = Left(Range("A1"), 4)
        Case 3: MyYear = Left(Range("A1"), 4)
        Case 4: MyYear = Left(Range("A1"), 4) - 1
    End Select
    MyPath = "\\server1\Finance\FS\Company 1\" & MyYear & "\Qtr " & MyQtr & "\" & Mymonth
    MsgBox MyPath
    
    End Sub

  4. #4
    Valued Forum Contributor ron2k_1's Avatar
    Join Date
    09-30-2009
    Location
    Not the USA
    MS-Off Ver
    Excel 2003, 2007
    Posts
    606

    Re: Programatically save in Quarter file based on "text-date" in cell

    So what I ended up doing is a table like the one on the attached file.

    I then entered a formula in D3 to give me the file path; as I'm not that good with VBA to enter all the formulas in the code itself, I know it's possible.

    I then entered this code and refer to D3 on the attachment
    Sub DeleteAllCode()
    Dim wb              As Workbook
    Dim x               As Integer
    Dim Proceed         As VbMsgBoxResult
    Dim Prompt          As String
    Dim Title           As String
        
        If Application.Version > 11 Then
        Set wb = ActiveWorkbook
            
            wb.BuiltinDocumentProperties("Comments") = "Created by " & Environ("USERNAME") & " on " & Format(Now, "mmm dd, yy hh:mm:ss AM/PM")
            wb.SaveAs Filename:=Sheets("Sheet1").Range("D3").Text & "\" & "FS Statements.xlsx", _
                FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            ActiveWorkbook.Close
        
        Else
            
            wb.BuiltinDocumentProperties("Comments") = "Created by " & Environ("USERNAME") & " on " & Format(Now, "mmm dd, yy hh:mm:ss AM/PM")
            wb.SaveAs Filename:=Sheets("Sheet1").Range("D3").Text & "\" & "FS Statements.xls", _
                FileFormat:=xlWorkbookNormal
                
        Prompt = "Are you certain that you want to delete all the VBA Code from " & _
                ActiveWorkbook.Name & "?"
        Title = "Verify Procedure"
        
        Proceed = MsgBox(Prompt, vbYesNo + vbQuestion, Title)
        If Proceed = vbNo Then
            MsgBox "Procedure Canceled", vbInformation, "Procedure Aborted"
            Exit Sub
        End If
        
        On Error Resume Next
        With ActiveWorkbook.VBProject
            For x = .VBComponents.Count To 1 Step -1
                .VBComponents.Remove .VBComponents(x)
            Next x
            For x = .VBComponents.Count To 1 Step -1
                .VBComponents(x).CodeModule.DeleteLines _
                        1, .VBComponents(x).CodeModule.CountOfLines
            Next x
        End With
        On Error GoTo 0
        
        End If
          
    End Sub
    So the code above do two things. As I want the tweaked file (after running all my other pieces of code) saved in a specific file path, I also don't want to save the VBA code. So I know that by saving in xlsx format all code is deleted. So I told excel to identify the excel version being used, if its 2007 and above to save as xlsx and all code is gone. Now, if the version is lower, then obviously it cannot save as xlsx. It can only save as xls file; but by doing so it retain all the vba code (which I don't want) so I told it to save as xls format in the path specified in D3 and remove all the vba code programatically.

    I've tested in excel 2010 and it works fine. I haven't yet tested in xl 2003. It'd be interesting how it handles the removal of the code....

    Now, how do I pat my own back... ?
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor
    Join Date
    08-13-2012
    Location
    Gardony, Hungary
    MS-Off Ver
    Excel 2003
    Posts
    558

    Re: Programatically save in Quarter file based on "text-date" in cell

    Wow dude, this is sick. Very different approach. :D I never knew VBA could remove code from the module. What the hell!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1