Solved

Open file with changing month

Posted on 2014-07-22
5
164 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now