+ Reply to Thread
Results 1 to 8 of 8

Link or Open file by date modified

Hybrid View

  1. #1
    Registered User
    Join Date
    03-02-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    2

    Link or Open file by date modified

    Hello all.

    I have file saved to the same location every hour. The file will always have the same column layout but the numbers of rows may increase or decrease.

    I want to import this data into another spreadsheet using a macro or linking directly in. However the name of the file with the data is different every time because it is saved with a date and time in its name.

    How can I open or import ONLY from the newest file in the directory?

    For example the file name is

    DailyReportSales.201003020758.csv

    Thanks

    Andy

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Link or Open file by date modified

    My suggestion is to have your import macro MOVE the .csv files into an "IMPORTED" folder after it has done its work. Then your macro need only import any file it finds appearing in the main "watch" directory. If it sees a file appear, it imports it and moves it to the "IMPORTED" folder.

    This alleviates all the overhead trying to parse filenames and/or file attributes.
    Last edited by JBeaucaire; 03-02-2010 at 12:10 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    03-02-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Link or Open file by date modified

    Hi,

    Thanks for taking the time to respond.

    If I am understanding you correctly I still have to manually move a file to a diffrent directory or I have to find a way for a macro to select the newest file and move it - but that kind of brings me back to the same point.

    I have seen some VBA code out there that does allow you to select a file by partial name - for example daily*.xls but I would then need have have some attribution code.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Link or Open file by date modified

    Quote Originally Posted by Burnsterboy View Post
    If I am understanding you correctly I still have to manually move a file to a different directory...
    Quote Originally Posted by JBeaucaire
    My suggestion is to have your import macro MOVE the .csv files
    Then you are not understanding me correctly. I suggested the macro you are using to import move the file when it's done with the import.

    The VBA function NAME() allows you to move a file to another location effectively by renaming it. I have several macros that do this...import all the files in a single folder and move them when done. Here's one for you to look over:
    PHP Code: 
    Sub Consolidate()
    'Author:     JBeaucaire'
    'Date:       9/15/2009     (2007 compatible)'
    'Summary:    Open all Excel files in a specific folder and merge data'
    '            into one master sheet (stacked)'
    '            Moves imported files into another folder'
    Dim fName As StringfPath As StringfPathDone As StringOldDir As String
    Dim LR 
    As LongNR As Long
    Dim wbkOld 
    As WorkbookwbkNew As Workbookws As Worksheet

    'Setup'
        
    Application.ScreenUpdating False
        Application
    .EnableEvents False
        Application
    .DisplayAlerts False
        
        Set wbkNew 
    ThisWorkbook
        wbkNew
    .Activate
        Sheets
    ("Master").Activate   'sheet report is built into'
        
        
    If MsgBox("Import new data to this report?"vbYesNo) = vbNo Then Exit Sub
        
        
    If MsgBox("Clear the old data first?"vbYesNo) = vbYes Then
            Cells
    .Clear
            NR 
    1
        
    Else
            
    NR Range("A" Rows.Count).End(xlUp).Row 1
        End 
    If

    'Path and filename'
        
    OldDir CurDir             'memorizes the current working path'
        
    fPath "C:\My Documents\Trackers\"
        fPathDone = "
    C:\My Documents\Trackers\Imported\"
        ChDir fPath
        fName = Dir("
    *.xl*")      'filtering key, change to suit

    'Import a sheet from found file'
        Do While Len(fName) > 0
            'Open file'
                Set wbkOld = Workbooks.Open(fName)
            'Find last row and copy data'
                LR = Range("
    A" & Rows.Count).End(xlUp).Row
                Range("
    A1:A" & LR).EntireRow.Copy _
                    wbkNew.Sheets("
    Master").Range("A" & NR)
            'close file'
                wbkOld.Close False
            'Next row'
                NR = Range("
    A" & Rows.Count).End(xlUp).Row + 1
            'move file to IMPORTED folder'
                Name fPath & fName As fPathDone & fName
            'ready next filename'
                fName = Dir
        Loop

    'Cleanup'
        ActiveSheet.Columns.AutoFit
        Application.DisplayAlerts = True
        Application.EnableEvents = True
        Application.ScreenUpdating = True

    'restores original working path'
        ChDir OldDir
    End Sub 
    The section of code of relevance is:
            'move file to "imported" folder
                Name fPath & fName As fPathDone & fName
    Naming a file and applying a different directory with the same filename effectively moves it. And this is far less hurdles to jump through than trying to decipher an ever-growing list of files for the latest addition.
    Last edited by JBeaucaire; 03-02-2010 at 01:48 PM.

  5. #5
    Registered User
    Join Date
    05-04-2009
    Location
    Burlington, USA
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Link or Open file by date modified

    I have the same problem as Burnsterboy, however, I can't move the files after I import them. I have to leave them in the one directory.

    The files I need have names that change depending on the date: YYMMDD_TC.csv and another named BX_YYMMDD.csv

    Any thoughts?

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Link or Open file by date modified

    Burnsterboy, if that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.
    =======

    FreshPrince, your post does not comply with forum rules #2 - do not post questions of your own in someone else's thread.

    Create your own thread and include a link to this one if you find it relevant. You'll need to provide more information anyway.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Link or Open file by date modified

    If there is only ONE of these dated files each day, then this would find and open that one file:
    Option Explicit
    
    Sub OpenDatedCSV2()
    Dim wbName As String, fPath As String
    
    fPath = "C:\2009\"      'remember the last \ in this string
    wbName = Dir("DailyReportSales." & Format(Date, "YYYYMMDD") & "*.csv")
    
    If wbName <> "" Then Workbooks.Open fPath & wbName
    
    End Sub
    Does this get you started?

  8. #8
    Registered User
    Join Date
    02-20-2009
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    92

    Re: Link or Open file by date modified

    JBeaucaire,

    What if the they need the most recent file, which may not be current date.

    I am referring to this line from your latest post:
    wbName = Dir("DailyReportSales." & Format(Date, "YYYYMMDD") & "*.csv")
    For instance, what if they need to run this early in the morning and the latest file is prior day? Or 3 days prior after a weekend, etc.

    Thats the particular problem I am having. I'll begin a new thread to discuss because it might be different from this topic.
    Last edited by starcraftbud; 04-20-2010 at 11:32 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1