Solved

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

Posted on 2014-09-23
12
195 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
Comment Utility
 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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Jagwarman
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 24

Accepted Solution

by:
Phillip Burton earned 250 total points
Comment Utility
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
Comment Utility
samo4fun

I now get Constant expression required

Regards
0
 
LVL 15

Assisted Solution

by:Haris Djulic
Haris Djulic earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Change

MonthName(3, True)

Open in new window


to

MonthName(Month(Date), True)

Open in new window

0
 

Author Closing Comment

by:Jagwarman
Comment Utility
Thanks to both Experts
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
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.

763 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

10 Experts available now in Live!

Get 1:1 Help Now