Solved

Open file with changing month

Posted on 2014-07-22
5
173 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:
GasperK earned 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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