Solved

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

Posted on 2014-09-23
12
198 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

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…
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 demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

809 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