Solved

Open file in folder where path will change..........

Posted on 2014-09-23
12
197 Views
Last Modified: 2014-09-23
I am using the below macro that opens the lastest file in a folder but....... my problem is that the path will change monthly and annually so where in the below Macro the path is

Const myDir As String = "c:\Refresh"

my path is P:\Euro\Cpn\Cpn Inv\2014\09 Sep 14 which means next month it will be P:\Euro\Cpn\Cpn Inv\2014\10 Oct 14 and Next Year P:\Euro\Cpn\Cpn Inv\2015\09 Sep 15 etc etc.

Is this doable?

Thanks in advance


Sub GetMostRecentFile()
   
    Dim FileSys As FileSystemObject
    Dim objFile As File
    Dim myFolder
    Dim strFilename As String
    Dim dteFile As Date
       
    'set path for files - change for your folder
    Const myDir As String = "c:\Refresh"
   
    'set up filesys objects
    Set FileSys = New FileSystemObject
    Set myFolder = FileSys.GetFolder(myDir)
       
   
    'loop through each file and get date last modified. If largest date then store Filename
    dteFile = DateSerial(1900, 1, 1)
    For Each objFile In myFolder.Files
        If objFile.DateLastModified > dteFile Then
            dteFile = objFile.DateLastModified
            strFilename = objFile.Name
        End If
    Next objFile
    Workbooks.Open strFilename
           
    Set FileSys = Nothing
    Set myFolder = Nothing
End Sub
0
Comment
Question by:Jagwarman
  • 5
  • 4
  • 3
12 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40338809
 Dim MyDate As Date
 MyDate = Now()
Debug.Print "P:\Euro\Cpn\Cpn Inv\" & Year(MyDate) & "\" & Format(MyDate, "mm mmm yy")

Open in new window

0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40338812
You can change the Debug.Print in line 3 for "myDir = ", i.e.

 Dim MyDate As Date, myDir as string
 MyDate = Now()
myDir.Print "P:\Euro\Cpn\Cpn Inv\" & Year(MyDate) & "\" & Format(MyDate, "mm mmm yy")

Open in new window


However, you cannot use:

Const myDir As String = 

Open in new window


A Constant cannot be calculated on something which varies - so you have to use a Dim, as in the above example.
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40338820
Hello,

you can use this :

Path = "P:\Euro\Cpn\Cpn Inv\" & Year(Date) & "\" & Format(Month(Date), "00") & " " & MonthName(3, True) & " " & Right(Year(Date), 2)
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:Jagwarman
ID: 40339087
Phillip,

when I use this

 Dim MyDate As Date
 MyDate = Now()
Debug.Print "P:\Euro\Cpn\Cpn Inv\" & Year(MyDate) & "\" & Format(MyDate, "mm mmm yy")

I get Varaible Not Defined on (myDir)

Set myFolder = FileSys.GetFolder(myDir)

When I try

Dim MyDate As Date, myDir as string
 MyDate = Now()
myDir.Print "P:\Euro\Cpn\Cpn Inv\" & Year(MyDate) & "\" & Format(MyDate, "mm mmm yy")


I get Type mismatch at myDir

myDir.Print "P:\
0
 

Author Comment

by:Jagwarman
ID: 40339092
sam4fun

when I use Path = "P:\Euro\Cpn\Cpn Inv\" & Year(Date) & "\" & Format(Month(Date), "00") & " " & MonthName(3, True) & " " & Right(Year(Date), 2)

I get Variable not defined at Path....

Path = "P:\Euro\
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40339123
Hi,

can you put it like this :

Const myDir As String ="P:\Euro\Cpn\Cpn Inv\" & Year(Date) & "\" & Format(Month(Date), "00") & " " & MonthName(3, True) & " " & Right(Year(Date), 2)

and then try
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 250 total points
ID: 40339154
Try

Dim MyDate As Date, myDir as string
 MyDate = Now()
myDir = "P:\Euro\Cpn\Cpn Inv\" & Year(MyDate) & "\" & Format(MyDate, "mm mmm yy")

Open in new window

0
 

Author Comment

by:Jagwarman
ID: 40339156
samo4fun

I now get Constant expression required

Regards
0
 
LVL 15

Assisted Solution

by:Haris Djulic
Haris Djulic earned 250 total points
ID: 40339188
Hi, again ;)

here is the updated code and tested...

Sub GetMostRecentFile2()
    
    Dim FileSys As FileSystemObject
    Dim objFile As File
    Dim myFolder
    Dim strFilename As String
    Dim dteFile As Date
        
    'set path for files - change for your folder
    Dim myDir As String
    myDir = "P:\Euro\Cpn\Cpn Inv\" & Year(Date) & "\" & Format(Month(Date), "00") & " " & MonthName(3, True) & " " & Right(Year(Date), 2)
    'set up filesys objects
    Set FileSys = New FileSystemObject
    Set myFolder = FileSys.GetFolder(myDir)
        
    
    'loop through each file and get date last modified. If largest date then store Filename
    dteFile = DateSerial(1900, 1, 1)
    For Each objFile In myFolder.Files
        If objFile.DateLastModified > dteFile Then
            dteFile = objFile.DateLastModified
            strFilename = objFile.Name
        End If
    Next objFile
    Workbooks.Open strFilename
            
    Set FileSys = Nothing
    Set myFolder = Nothing
End Sub

Open in new window

0
 

Author Comment

by:Jagwarman
ID: 40339243
samo4fun, very odd it's looking for a folder 09 Mar 14

it is something to do with & MonthName(3, True) because if I change 3 to 2 it looks for Feb etc
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40339258
Change

MonthName(3, True)

Open in new window


to

MonthName(Month(Date), True)

Open in new window

0
 

Author Closing Comment

by:Jagwarman
ID: 40339291
Thanks to both Experts
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvieā€¦
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabilā€¦
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.

786 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