Results 1 to 4 of 4

Open an xlsx with most recent date in filename, run my data pulling macro, then move file

Threaded View

alanwu07 Open an xlsx with most recent... 01-30-2013, 11:30 PM
patel45 Re: Open an xlsx with most... 01-31-2013, 03:30 AM
AlphaFrog Re: Open an xlsx with most... 01-31-2013, 03:32 AM
alanwu07 Re: Open an xlsx with most... 01-31-2013, 05:24 AM
  1. #1
    Registered User
    Join Date
    01-30-2013
    Location
    Hong Kong
    MS-Off Ver
    Excel 2007
    Posts
    5

    Open an xlsx with most recent date in filename, run my data pulling macro, then move file

    Hi,

    I'm currently trying to create a master list of prices of a product that changes from day to day. In order to do this more efficiently, I want to write a macro that will pull data from the daily workbook (always formatted in the same way eg: 31 Jan 2013.xlsx) that I download to a specific path every morning. Once the data has been pulled, I want the workbook to close and be moved to an archive folder. I have tried various codes that I've found on here for the file opening. I understand that I have to write something that will take Date and search for it in DD MMM YYYY format and loop it while subtracting 1 day from it each time. Here is the code I am using so far for the file opening:

    Sub Macro1()
    '
    ' Macro1 Macro
    '
    Dim TheString As String, TheDate As Date
    Dim ThePath As String, TheBook As Workbook
    Set TheBook = ThisWorkbook
    
    TheDate = Date
    ThePath = "C:\Daily Prices\"
    TheString = WorksheetFunction.Text(TheDate, "DD MMM YYYY") & ".xlsx"
    
    On Error Resume Next
    
    Do
    Workbooks.Open ThePath & TheString
    
        If Err <> 0 Then
            TheDate = Date - 1
            TheString = WorksheetFunction.Text(TheDate, "DD MMM YYYY") & ".xlsx"
            Err = 0
        Else
            On Error GoTo 0
            Exit Do
        End If
    Loop Until ActiveWorkbook.Name <> ThisWorkbook.Name
    
    '
    End Sub
    The code above seems to get stuck in the loop and can not open the file. I typically only have one file in the folder (the current day) so I can adjust it to work exclusively for the current day, but I get these prices sometimes on a Sunday and I'll have to do Date - 1 on Monday. Can someone help me fix this one?

    Also will need help with how to refer to the opened file once it's been opened as my data pulling macro pulls from over 10 different groups of cells so the code switches the Active workbook back and forth between the master and the daily. Finally the file moving as well to path: C:\Daily Prices\Archived.

    Sorry for the long post, I want to make sure I cover all the bases in one post.
    Last edited by alanwu07; 01-31-2013 at 05:25 AM.

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