Solved

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

Posted on 2014-09-23
12
199 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

726 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