Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open file by creation date with VBA

Status
Not open for further replies.

MVD100

Technical User
Dec 10, 2008
49
US
I am using the following to get the path and name of a file to open:
Code:
StrFileName = Application.GetOpenFilename(FileFilter:="CSV Files (*.csv),*.csv", _
    Title:="Select File To Open")
Because I know the beginning name of the file, I need only to open a file with a given creation date value. For example, there are multiple log files: file1.csv, file2.csv, file3.csv... Using the code above I would see all files and without dates. I want the user to only enter the date. Say, I want to open the log file that was created on a day last week. The VBA code would open all the log files for the given date and import them into Excel.

Here is my pseudo code:
Code:
Open StrFileName In StrFolderPath.Files Where _
  StrFileName.DateCreated Is StrDate
 


Hi,

I would advise against OPENING a text file using the Open Method, in most cases. You can get unexpected results that you have no control over.

Rather, i would advise using the DATA IMPORT feature, where you have control over each column's data type.

You can use the the GetOpenFileName method to get the PATH and just concatenate the date as required, using the Format function to convert the Date to a string.

If you choose to opent the file using the DATA IMPORT feature, please indicate. Otherwise ...
Code:
Workbooks.Open StrFolderPath & "\" & StrFileName & Format(Date, "yyyy-mm-dd")


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
To select files by date created you could use the FileSystemObject. I used it recently - see: thread707-1628719


Gavin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top