Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA to open file where Path Year and Month will change

Posted on 2014-03-20
4
Medium Priority
?
2,707 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 52

Accepted Solution

by:
Rgonzo1971 earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.
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‚Ķ

715 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