Update based on created date (user input)


Posted by Alexia on January 03, 2002 7:39 AM

I am writing a macro that will look for all files in a given directory that have been created after a certain date and then extract certain information into the summary.

How can I write this so that the user will be prompted for the created date. I want it to lie within 2 dates actually, so a staring date and an ending date.

Thanks :-)

Posted by Dank on January 03, 2002 9:01 AM

This code should help you out. VBA has a built in object for finding files but it doesn't allow you to specify any date ranges. This code uses the FileSystem object. To use it you must click Tools, References and select Microsoft Scripting Runtime otherwise you'll get an error. This is one possible way of many of how to do this. Please let me know if you have any problems.

Sub GetFiles()
Dim dteStart As Date, dteEnd As Date, strFolder As String
Dim fsObj As New Scripting.FileSystemObject, fsFile As Scripting.File
Dim fsFolder As Scripting.Folder, clnExcelFiles As New Collection


On Error GoTo ErrHandler:
'Does not include any error checking e.g. valid path. You'll need to add this.

strFolder = InputBox("Please enter folder path", "Path")
dteStart = InputBox("Please enter start date", "Start date")
dteEnd = InputBox("Please enter end date", "End date")

Set fsFolder = fsObj.GetFolder(strFolder)

For Each fsFile In fsFolder.Files
If fsFile.DateCreated >= dteStart And fsFile.DateCreated <= dteEnd And fsFile.Type = "Microsoft Excel Worksheet" Then
clnExcelFiles.Add fsFile.Name
End If
Next

'the collection clnExcelFiles now includes all Excel files created within the
'specified parameters. You can process them doing something like this:-

For Each i In clnExcelFiles
Workbooks.Open i
'Process the workbook
Next

Exit Sub
ErrHandler:
'An error occured.

End Sub

Posted by Alexia on January 03, 2002 7:50 PM

Thank you Dank, this was a tremendous help!!!

One followup question. Would I need to Dim i As Workbook for that last part to work? Thanks :-)



Posted by Dank on January 04, 2002 3:51 AM

You don't need to Dim I as workbook because it is actually used to refer to an item in a collection. Dim it as a variant.

Secondly, change the line
clnExcelFiles.Add fsFile.Name

to

clnExcelFiles.Add fsFile.Path

HTH,
Daniel.