Solved

VBA to open file where Path Year and Month will change

Posted on 2014-03-20
4
1,883 Views
Last Modified: 2014-03-20
I am using the below vba code that was supplied by robhenson to open files where the Year and date of the file changes. Howver I now need to add a further dimension to this to include the Month in the path [03 Mar, 04 Apr etc] I have tried to make the change myself but have faild miserably. Can an expert out there please provide the answer.

i.e. path is G:\Sales\Recon\Results\2014\03 Mar\dd.mm.yyyy.xls

The code i have is

Sub OpenFile()

FileYear = Year(Date)

MonthOffset = 0
If Day(Date) = 1 Then MonthOffset = 1
FileMonth = Month(Date) - MonthOffset
FileDate = Format(Date - 1, "dd.mm.yyyy")
FilePath = "G:\Sales\Recon\Results\" & FileYear & "\" & FileDate & ".xls"

Workbooks.Open (FilePath)

End Sub

Thanks
0
Comment
Question by:Jagwarman
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39941974
Hi,

pls try

Sub OpenFile()

FileYear = Year(Date)

strFileMonth = Format(Date - 1, "MM MMM")
FileDate = Format(Date - 1, "dd.mm.yyyy")
FilePath = "G:\Sales\Recon\Results\" & FileYear & "\" & strFileMonth & "\" & FileDate & ".xls"

Workbooks.Open (FilePath)

End Su

Open in new window

Regards
0
 
LVL 4

Expert Comment

by:senthilkumarsb
ID: 39941979
It works for me your code is good.
0
 
LVL 7

Expert Comment

by:jaynee
ID: 39941981
Can I say that I always encourage people to NOT use spaces or dots (fullstops, periods) in folder names.  It would be better to just use G:\Sales\Recon\Results\2014\03\ddmmyyyy.xls

I also can't see why you're subtracting a monthoffset and also 1 day from the date.

However, try replacing your "FilePath = " line with this, for what you asked for:

FilePath = "G:\Sales\Recon\Results\" & FileYear & "\" & FileMonth & " " & Format(ThisDate, "mmm") & "\" & FileDate & ".xls"
0
 
LVL 7

Expert Comment

by:jaynee
ID: 39941985
Ah - I should have refreshed the page, RGonzo1971 got there first!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

863 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

26 Experts available now in Live!

Get 1:1 Help Now