Solved

Open file with changing month

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
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…

739 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