+ Reply to Thread
Results 1 to 34 of 34

Using VBA to Open a File Name that changes slightly

  1. #1
    Registered User
    Join Date
    05-22-2009
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    47

    Using VBA to Open a File Name that changes slightly

    Hi All,

    I want to use vba to open a file where the name changes slightly each week it comes in. Let's say the file name is f:/Folder A 05212009

    I can do this to open it:
    Please Login or Register  to view this content.
    But, next week when the file name is Folder A 05282009, it won't open. Is there any way to write the code so that it picks up this pattern and can open it each week?

    I greatly appreciate any help you can give me!
    Last edited by souljive99; 05-26-2009 at 05:47 PM. Reason: added code tag

  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: Using VBA to Open a File Name that changes slightly

    Try something like this...it will search ThePath you designate for the filenames in the format you named. It will start with Today's date and work backward until finds a file and opens it...then it will stop.
    Please Login or Register  to view this content.
    _________________
    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
    05-22-2009
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Using VBA to Open a File Name that changes slightly

    Thank you very much, but this isn't working. Can I give you the exactly file names? The folder is C:\Documents and Settings\trevor\Desktop and the file name is "test 05.26.2009.xls"

    I like the idea of starting with today's date and running back through, but it doesn't seem to be working....

  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: Using VBA to Open a File Name that changes slightly

    That's because your original post demonstrated a filename format that does not match your need. Always best to start with ACTUAL need rather than something you'll have to interpret.

    Here's the code amended to your current example. I've highlighted the portions in red where you could make adjustments:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    05-22-2009
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Using VBA to Open a File Name that changes slightly

    It just sits with the hour glass and when I click Escape then debug, the part with "If Err <> 0 Then" is highlighted

  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: Using VBA to Open a File Name that changes slightly

    Try running through the code in the VBEditor using the F8 key to issue one command at a time... When it highlights that line of code, put your mouse over the Err variable and see what the current value is.

    I just used that code 3 times in a row to open varying files off of my computer. You could also triple check the path you gave me.

  7. #7
    Registered User
    Join Date
    05-22-2009
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Using VBA to Open a File Name that changes slightly

    Cool! This worked when I tried it again...Thank you so much...Actually have a follow up - can I use that same method to look into the most recent folder? The date format is YYYY_MM_DD

    Basically, I'd like to open up a folder like the following:

    C:\Holdings\2009_05_26

    Thanks again for your help.

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

    Re: Using VBA to Open a File Name that changes slightly

    Open it when? For what purpose? As part of this macro?

    You already see the formatting in the macro I've presented. You can play with ThePath variable the same way.
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    05-22-2009
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Using VBA to Open a File Name that changes slightly

    Do you do separate loops for the path and the string, like this? or is this wrong...

    Please Login or Register  to view this content.
    Last edited by souljive99; 05-26-2009 at 05:42 PM. Reason: added code tags

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using VBA to Open a File Name that changes slightly

    souljive,

    Please edit your post to add code tags.
    Entia non sunt multiplicanda sine necessitate

  11. #11
    Registered User
    Join Date
    05-22-2009
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Using VBA to Open a File Name that changes slightly

    Sorry, I'm new...Is this how you tag codes?

    Do you do separate loops for the path and the string, like this? or is this wrong...



    Please Login or Register  to view this content.
    Last edited by souljive99; 05-26-2009 at 05:42 PM. Reason: added code tags

  12. #12
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using VBA to Open a File Name that changes slightly

    No, it's not. The forum rules explain it, and so does the How To ... link in the menu bar.

  13. #13
    Registered User
    Join Date
    05-22-2009
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Using VBA to Open a File Name that changes slightly

    Sorry...hope this is right.

    Anyway, I am not sure if you have do this loop for the path and the string or once for both...if someone could take a look at this and let me know what I am doing wrong I would really appreciate it. Thanks.

    Please Login or Register  to view this content.

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

    Re: Using VBA to Open a File Name that changes slightly

    SJ, stop adding new posts with the same code. The moderators want you to go back and add those code tags to the PREVIOUS posts. You need to fix all of them, even the code in post #1.

    ==========
    Also, you took my code idea and moved it around. What I gave you in post #8 was in a different order than what you put in post #9. You can't use the variable TheDate before you've even given it a value...

    And your question really confuses me. You're telling me that would have FOLDER names with dates on them and then files INSIDE the folder that don't go WITH the same date? Isn't that intrinsically problematic?

  15. #15
    Registered User
    Join Date
    05-22-2009
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Using VBA to Open a File Name that changes slightly

    I apologize...I'm new to this, I'm trying to post things the right way. I think I understand now. Sorry for the error - I really do appreciate all the help.

    What I am trying to do is look for the most recent folder that has a date in it, say :

    F:\holdings\2005_05_21

    Then, in that folder I want to find the specified file, which is in this format:

    F:\holdings\2005_05_21\portfolio 05.21.2009.xls

    Of courses, those dates change every week. I want to open that file so I can change it around with a macro and save it again with a different name.

    As I said before - I really appreciate all the help and sorry for the multiple posts.

  16. #16
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Using VBA to Open a File Name that changes slightly

    souljive, you broke the code, thank you.

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

    Re: Using VBA to Open a File Name that changes slightly

    Did you mean to demonstrate year in 2005 for the folder and a year of 2009 for the file? I'm going to presume that was an error and just make them the same:
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 05-26-2009 at 07:56 PM.

  18. #18
    Registered User
    Join Date
    05-22-2009
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Using VBA to Open a File Name that changes slightly

    I'm using the f:\ drive. Do I need to use a Change Directory function or should it work? I copied your script exactly and its not picking it up.

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

    Re: Using VBA to Open a File Name that changes slightly

    Find the two references to the C:\ drive in the macro and change them accordingly.

  20. #20
    Registered User
    Join Date
    05-22-2009
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Using VBA to Open a File Name that changes slightly

    In the vba script, when I put the curser over the words TheDate it says "TheDate = 4/28/1996" and when I put the curser over Date it says "Date = 2/2/09" I think this may be why it's not working. Do you know what could be wrong? Thanks.

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

    Re: Using VBA to Open a File Name that changes slightly

    In your VBEditor, press CTRL-G to bring up the Immediate window. Then enter these two lines of code one at a time into the window and see if the msgbox that pops up has the correct date or not:
    Please Login or Register  to view this content.
    If you want to work on this in realtime, my Skype ID is devtuxx7069. Just click the green checkmark across from name under the Location: info.

    I'm sure there's something simple tweaked on your system somewhere, the code is pretty basic.

  22. #22
    Registered User
    Join Date
    05-22-2009
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Using VBA to Open a File Name that changes slightly

    Unfortunately I can't use skype on this computer.

    This is the code I am using:

    Please Login or Register  to view this content.
    These are the full file names:

    folder - "C:\holdings\2009_05_10"

    file - "portfolio 05.10.2009.xls"

    When I did the immediate msgbox to bring up today's date, it worked (it brought up 5/26/2009)

    Whenever I run the macro the hourglass just continues to run and when I hit escape and debug, the If Err <> 0 part is highlighted...any ideas?

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

    Re: Using VBA to Open a File Name that changes slightly

    Sorry SJ. The code looks fine. It opens a file for me when I create the folder and file on my system.

    The only unknown is your computer.

  24. #24
    Registered User
    Join Date
    05-22-2009
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Using VBA to Open a File Name that changes slightly

    I think I figured out the problem but I don't know the solution...

    When I put the date format as YYYY_MM_DD it picks up the date as 2009 5 26

    Any idea on how to fix this so it picks it up as 2009_05_26 (with the underscores and the 0 in front of the 5)

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

    Re: Using VBA to Open a File Name that changes slightly

    OK, something about that pesky underscore doesn't want to evaluate inside the TEXT function, so I cheated. I used another character (¢) and then used a substitute function to change it INTO an underscore. Problem solved.

    Please Login or Register  to view this content.

  26. #26
    Registered User
    Join Date
    05-22-2009
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Using VBA to Open a File Name that changes slightly

    Thanks! That works well...only problem is that the hour glass remains on the screen after the file is opened...so I can't continue the script to alter the opened excel sheet. Shouldn't the loop until...part do that?

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

    Re: Using VBA to Open a File Name that changes slightly

    Interesting. It stops on my system. We can add a "stop" command I suppose. Once more...:

    Please Login or Register  to view this content.

  28. #28
    Registered User
    Join Date
    08-15-2012
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Using VBA to Open a File Name that changes slightly

    Hello,

    This is a really useful conversation. I have learned a lot from reading it. I am also relatively new and have a related question I thoink could be answered realtively easily (or so I hope!).

    I am trying to not only open a file that has a date component that is important, but also has a time stamp that I want to ignore.

    For example, the file from today could either be 20120904_0900.xls (if it was run at 9AM) or 20120904_0843 (if it was run at 8:43 AM). I would like to open it whatever time it gets produced. I know it has something to do with the * function (like *.xls to open or copy every excel sheet in a folder) I have seen so often on this site but I cannot quite figure out exactly how to do it.

    Any help would be greatly appreciated.

    Thanks!

    Shannon

  29. #29
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Using VBA to Open a File Name that changes slightly

    Hello slaneues, and welcome to the forum.

    Unfortunately you have inadvertently broken one of the forum rules. Please read the following and make the necessary change. Thanks.

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.

  30. #30
    Registered User
    Join Date
    08-15-2012
    Location
    USA
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Using VBA to Open a File Name that changes slightly

    Sorry! I am relatively new and forgot some of the rules. I found a solution to this, but thanks anyway!

    Shannon
    Last edited by JBeaucaire; 09-05-2012 at 03:24 PM. Reason: Removed whole post quotation

  31. #31
    Valued Forum Contributor
    Join Date
    11-20-2012
    Location
    Seattle, WA USA
    MS-Off Ver
    Excel 2010
    Posts
    597

    Re: Using VBA to Open a File Name that changes slightly

    Is there any way that we can set up the code skip the message box that says "Path\FileName.xls cannot be found. Check your spelling or try a different path." so it will auto loop until it opens, otherwise if it is a monthly document you have to hit ok 30 times until it opens

  32. #32
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Using VBA to Open a File Name that changes slightly

    Scott,

    Unfortunately you need to post your question in a new thread, it's against the forum rules to post a question in the thread of another user. If you create your own thread, any advice will be tailored to your situation so you should include a description of what you've done and are trying to do. Also, if you feel that this thread is particularly relevant to what you are trying to do, you can surely include a link to it in your new thread.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  33. #33
    Registered User
    Join Date
    11-05-2013
    Location
    Stuttgart
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: Using VBA to Open a File Name that changes slightly

    Hello,

    I am also trying to do the same thing. But instead of an .xls file it a PowerPoint one. I am using a macro to add a PowerPoint file as an object.
    The file's name is also a date. What is the equivalent of WorksheetFunction.Text(TheDate, "YYYY_MM_DD") function in VBA for PowerPoint?

    I would appreciate your help.

  34. #34
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Using VBA to Open a File Name that changes slightly

    halaami8

    Unfortunately you need to post your question in a new thread, it's against the forum rules to post a question in the thread of another user. If you create your own thread, any advice will be tailored to your situation so you should include a description of what you've done and are trying to do. Also, if you feel that this thread is particularly relevant to what you are trying to do, you can surely include a link to it in your new thread.
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

+ 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