+ Reply to Thread
Results 1 to 6 of 6

VBA open file with variable name with a variable file path.

  1. #1
    Registered User
    Join Date
    02-08-2017
    Location
    Amsterdam
    MS-Off Ver
    microsoft excel 2007
    Posts
    8

    VBA open file with variable name with a variable file path.

    Hi guys,

    I need some help. I'm trying to write a VBA code that opens a CSV file but there are two problems.

    Problem 1:

    The folder in which the CSV file is located changes daily based on today's date, so the file path will look something like this:

    On 07-02-2018: C:/File/20180207/ CSV File
    On 08-02-2018: C:/File/20180208/ CSV File
    On 09-02-2018: C:/File/20180209/ CSV File
    On 10-02-2018: C:/File/20180210/ CSV File

    Etc. The location is basically today's date, YYYYMMDD without any hyphens or slashes.

    Problem 2:

    The filename of CSV file that needs to be opened also changes everyday but it always ends with the same suffix (Done), so for example:

    123Done.CSV
    424Done.CSV
    543Done.CSV
    463Done.CSV
    532Done.CSV

    I've managed to solve the second problem using the following code.

    Dim fname As Variant
    Dim myPath As String

    myPath = "C:\FILE\20180207\"
    fname = Dir(myPath & "*DONE.CSV")

    If fname <> "" Then
    Workbooks.Open (myPath & fname)
    End If

    The only problem is that obviously tomorrow my code won't work as the file needed will be located at C:\FILE\ 20180208\ instead of C:\FILE\ 20180207\.

    Any way to set the filepath to C:\FILE\(Today's date in format YYYYMMDD) ?
    Last edited by BLTnoBacon; 07-02-2018 at 02:23 PM.

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Re: VBA open file with variable name with a variable file path.


    Hi !

    As per those variable elements just pick up the file with GetOpenFilename method, to see in VBA inner help …
    Last edited by Marc L; 07-02-2018 at 12:44 PM.

  3. #3
    Registered User
    Join Date
    02-08-2017
    Location
    Amsterdam
    MS-Off Ver
    microsoft excel 2007
    Posts
    8

    Re: VBA open file with variable name with a variable file path.

    Hi Marc!

    Thanks for the quick reply. But I don't want the user to be able to select the file themselves. Is there any way to open the file through VBA?

  4. #4
    Registered User
    Join Date
    02-08-2017
    Location
    Amsterdam
    MS-Off Ver
    microsoft excel 2007
    Posts
    8

    Re: VBA open file with variable name with a variable file path.

    I've managed to solve the second problem using the following code.

    Dim fname As Variant
    Dim myPath As String

    myPath = "C:\FILE\20180207\"
    fname = Dir(myPath & "*DONE.CSV")

    If fname <> "" Then
    Workbooks.Open (myPath & fname)
    End If

    The only problem is that obviously tomorrow my code won't work as the file needed will be located at C:\FILE\ 20180208\ instead of C:\FILE\ 20180207\.

    Any way to set the filepath to C:\FILE\(Today's date in format YYYYMMDD) ?
    Last edited by BLTnoBacon; 07-02-2018 at 02:23 PM.

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: VBA open file with variable name with a variable file path.


    Yes just using Format VBA function …

    But what will happen if one day the final user can't work ?
    For example friday he slips on a dog poo and its ankle hurts so he can't go at work until tuesday.
    So with your way how will he resolve its need for last friday and monday ?!

    Obviously it's very not a concern with my way …

  6. #6
    Registered User
    Join Date
    09-13-2012
    Location
    Queensland
    MS-Off Ver
    Excel 2007
    Posts
    36

    Re: VBA open file with variable name with a variable file path.

    Hi Bacon

    Maybe use VBA.format to create correct definition of the current day

    Please Login or Register  to view this content.

+ 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. Macro to open specified file path with variable date
    By nelsonr15 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-01-2013, 06:57 PM
  2. [SOLVED] I want to set ThisWorkbook.Path inside a variable in order to open a file.
    By grid in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-21-2013, 11:07 AM
  3. Replies: 0
    Last Post: 01-27-2013, 12:13 PM
  4. [SOLVED] Macro - Master file to import data from another open file with variable file name
    By jdodz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-10-2012, 10:56 PM
  5. [SOLVED] Open file from a folder and assign that file to an Object variable
    By pratyu in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-22-2012, 02:11 AM
  6. Variable File Path
    By kbruin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-11-2011, 02:26 AM
  7. Place a File Path and File as a Variable
    By SEOT in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-21-2008, 11:27 AM

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