Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Open file with changing month

Posted on 2014-07-22
5
Medium Priority
?
178 Views
Last Modified: 2014-07-23
So, i have a file zyz.xls

its store in

Q:\Debt\Inputs\2014\201406\Reporting

Can i get some code that looks for the folder from last month, so the process is always ran after month end. So in august, we run July files, so the code would be looking for

Q:\Debt\Inputs\2014\201407\Reporting

Is this possible through a date function?

Thanks
0
Comment
Question by:Seamus2626
  • 3
5 Comments
 
LVL 10

Accepted Solution

by:
Gašper Kamenšek earned 2000 total points
ID: 40210959
Here's what you need

    
sub test()
    XYear = Year(Date)
    XMonth = Month(Date) - 1
    XMonth2 = Format(XMonth, "00")
    MyDate = XYear & XMonth2
    MsgBox MyDate

Open in new window

End Sub
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40211176
Note:
Just for reminder and reference only, It should use DateAdd() for previous month and previous year when the Month(Date) is equal 1, and Month(Date)-1 that will be 0 for January for previous month

I try this example date at Range("A1")="23/01/2014", please see the code  based on previous post code and code for Format(DateAdd("m", -1, Range("A1")), "mm") for previous month.

Duncan  

Sub test()
    Dim XYear As String
     Dim Xmonth As String
     Dim Xmonth2 As String
     Dim MyDate As String
     Dim str As String
     Dim tmp As String
     Dim res As String
     Range("A1") = "23/01/2014"
     Range("A1").NumberFormat = "dd/mm/yyyy"
     tmp = DateValue(Range("A1"))
    XYear = Year(tmp)
    Xmonth = Month(tmp) - 1
    Xmonth2 = Format(Xmonth, "00")
    MyDate = XYear & Xmonth2
    Debug.Print "===================="
    Debug.Print MyDate
    
    If Month(tmp) = 0 Then
res = "File-1:Q:\Debt\Inputs\" & Year(Range("A1")) & "\" & Year(Range("A1")) & Format(DateAdd("m", -1, tmp), "mm") & "\Reporting"
    Else
res = "File-2: Q:\Debt\Inputs\" & Format(DateAdd("yyyy", -1, Range("A1")), "yyyy") & "\" & Format(DateAdd("yyyy", -1, Range("A1")), "yyyy") & Format(DateAdd("m", -1, Range("A1")), "mm") & "\Reporting"

    End If
Debug.Print res
    End Sub

Open in new window

0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40214526
Any comment , Seamus2626 & GasperK ?


Please advise


Duncan
0
 

Author Comment

by:Seamus2626
ID: 40214597
Hi Duncan,

Ive beeen using the following

Sub OpenFileCross_Pipeline_Staff()

FileYear = Year(Date)

MonthOffset = 0
If Day(Date) = 1 Then MonthOffset = 1
FileMonth = Month(Date) - MonthOffset
FileDate = Format(Date - 1, "yyyymm")
FilePath = "R:\SPM\Monthly_Inputs\" & FileYear & "\" & FileDate & "\Reporting\PCM_Pipeline_Analysis_Staff_raw.xlsx"

Workbooks.Open (FilePath)

End Sub


Thanks for your add
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40214609
if so, why others will use DateAdd() on VBA ?
Actually I guess we can use other way to do that without Month() and DateAdd() assist if we want to , Right ?

Anyway, just reminder and note only

Duncan
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Microsoft's Excel has many features that most people will never need nor take advantage of.  Conditional formatting is one feature that you may find a necessity once you start using it.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

963 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