Solved

VBA to open file where Path Year and Month will change

Posted on 2014-03-20
4
2,287 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
[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
  • 2
4 Comments
 
LVL 51

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

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…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
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…

737 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