+ Reply to Thread
Results 1 to 4 of 4

Creating Code to open a file whose name changes by week

  1. #1
    Registered User
    Join Date
    10-23-2014
    Location
    Louisville, KY
    MS-Off Ver
    Office 2013
    Posts
    2

    Creating Code to open a file whose name changes by week

    I want to create code that will open up an excel workbook that has a different name every week. In the file path it is "we 11-08-14" I want to be able to open that folder up and select a filename based on wildcard values included around the day I want to access. This is what I have so far. It is compiling it successfully but it won't open the document. I suspect an error in the directory.

    Sub ImportOpenWK()

    Dim directory As String
    Dim fileName As String
    Dim foldername As String
    Dim sheet As Worksheet
    Dim total As Integer
    Dim weeknum As Integer
    Dim dayser As Long
    Dim weekof As Long
    Dim convertser As Date
    Dim wbk As Workbook


    Application.ScreenUpdating = False
    Application.DisplayAlerts = False


    weeknum = Format$(Now(), "ww", vbSaturday)
    dayser = DateSerial(2014, 1, 4)
    weekof = dayser - 7 + 7 * weeknum
    convertser = weekof
    convertser = Format$(weekof, "mm-dd-yy")



    directory = "S:\District\WP\All\H2K\Planning\Plans\Lineups\we&convertser"
    fileName = Dir(directory & "Monday Lineup??")

    Do While fileName <> ""
    Set wbk = Workbooks.Open(directory & fileName)
    wbk.Worksheets("Imported Data").Range("A2:N10000").Copy
    Worksheets("This Week SP").Range("A2").PasteSpecial (xlPasteValues)
    Application.CutCopyMode = False

    Workbooks(fileName).Close

    fileName = Dir()


    Loop
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True

    End Sub

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Creating Code to open a file whose name changes by week

    Hi dafigu01 and welcome to ExcelForum,

    When you post code, you need 'code tags'. See the forum rules for instructions:
    http://www.excelforum.com/forum-rule...rum-rules.html

    We're not allowed to answer your questions without 'Code Tags'.

    I think you have two problems:
    a. The date has to be a string to be formatted the way you want
    b. The directory should probably have a trailing backslash "\".

    Run this in the debugger and open the Immediate Window (CTRL G). The two debug.print lines should give you a clue to whether the directory has the correct name, and if the Dir() command found the file you are looking for.

    Lewis

    Changes in red:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    10-23-2014
    Location
    Louisville, KY
    MS-Off Ver
    Office 2013
    Posts
    2

    Re: Creating Code to open a file whose name changes by week

    LJMetzger,

    Thanks for the quick response I will be sure to employ code tags in any future posts. The changes you made allowed me to fix the code.

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Creating Code to open a file whose name changes by week

    I'm glad it worked out for you. Don't hesitate to ask if you have any more problems and/or questions.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 0
    Last Post: 10-02-2013, 01:47 AM
  2. [SOLVED] Code to open another excel file, file name depending on cell contents
    By L-Drr in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-02-2013, 11:09 AM
  3. [SOLVED] Open file from location using VBA code - file name changes everytime.
    By Swapnil.Kupwade in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-31-2013, 11:48 AM
  4. Replies: 3
    Last Post: 05-21-2013, 08:22 AM
  5. VBA for excel 2003. Open file dialog box, open 2007 file xlsx, continue with code
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-25-2011, 12:12 AM

Tags for this Thread

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