Solved

Open file with changing month

Posted on 2014-07-22
5
175 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
[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 10

Accepted Solution

by:
Gašper Kamenšek 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
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.
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

636 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