VBA: Open Most Current File In Specified Folder

I'm setting up a module that will grab a specific .xls and organize it
for production as well as load the information into Crystal for
archiving.  In the past, I have always directed the user to save
the .xls as "AnyFileName" in a specific folder and the module would
pick it up at a scheduled time.  This has me relying on the user to
not make any mistakes in naming the file.

Is there a produre in VBA that will open the most current file in a
specified folder?
0
whicks (14)
6/27/2008 6:54:11 PM
excel 39879 articles. 2 followers. Follow

2 Replies
585 Views

Similar Articles

[PageSpeed] 3

The macro below should work OK.

HTH,
Bernie
MS Excel MVP


Sub FindNewestFile()
With Application.FileSearch
   '// The following is a workaround that has worked for Dana DeLouis
   '// Wake Excel up for msoSortByLastModified to work !
   .NewSearch
   .LookIn = "C:\"
   .FileName = "*.xls"
   .Execute SortBy:=msoSortBySize
End With
With Application.FileSearch
   .NewSearch
   'Change this to your directory
   .LookIn = "C:\Documents and Settings\UserName\My Documents\Excel"
   .SearchSubFolders = False
   .FileType = msoFileTypeExcelWorkbooks
   If .Execute(SortBy:=msoSortByLastModified, SortOrder:=msoSortOrderDescending) > 0 Then
      MsgBox "The newest file is " & .FoundFiles(1) & " created " & FileDateTime(.FoundFiles(1))
       Workbooks.Open .FoundFiles(1)
   Else
      MsgBox "There were no files found."
   End If
End With
End Sub

"whicks" <whicks@kpmg.com> wrote in message 
news:e01dc9c6-e90e-4e37-b733-3ac7448bb15c@e39g2000hsf.googlegroups.com...
> I'm setting up a module that will grab a specific .xls and organize it
> for production as well as load the information into Crystal for
> archiving.  In the past, I have always directed the user to save
> the .xls as "AnyFileName" in a specific folder and the module would
> pick it up at a scheduled time.  This has me relying on the user to
> not make any mistakes in naming the file.
>
> Is there a produre in VBA that will open the most current file in a
> specified folder? 


0
Bernie
6/27/2008 7:21:39 PM
On Jun 27, 2:21=A0pm, "Bernie Deitrick" <deitbe @ consumer dot org>
wrote:
> The macro below should work OK.
>
> HTH,
> Bernie
> MS Excel MVP
>
> Sub FindNewestFile()
> With Application.FileSearch
> =A0 =A0'// The following is a workaround that has worked for Dana DeLouis
> =A0 =A0'// Wake Excel up for msoSortByLastModified to work !
> =A0 =A0.NewSearch
> =A0 =A0.LookIn =3D "C:\"
> =A0 =A0.FileName =3D "*.xls"
> =A0 =A0.Execute SortBy:=3DmsoSortBySize
> End With
> With Application.FileSearch
> =A0 =A0.NewSearch
> =A0 =A0'Change this to your directory
> =A0 =A0.LookIn =3D "C:\Documents and Settings\UserName\My Documents\Excel=
"
> =A0 =A0.SearchSubFolders =3D False
> =A0 =A0.FileType =3D msoFileTypeExcelWorkbooks
> =A0 =A0If .Execute(SortBy:=3DmsoSortByLastModified, SortOrder:=3DmsoSortO=
rderDescending) > 0 Then
> =A0 =A0 =A0 MsgBox "The newest file is " & .FoundFiles(1) & " created " &=
 FileDateTime(.FoundFiles(1))
> =A0 =A0 =A0 =A0Workbooks.Open .FoundFiles(1)
> =A0 =A0Else
> =A0 =A0 =A0 MsgBox "There were no files found."
> =A0 =A0End If
> End With
> End Sub
>
> "whicks" <whi...@kpmg.com> wrote in message
>
> news:e01dc9c6-e90e-4e37-b733-3ac7448bb15c@e39g2000hsf.googlegroups.com...
>
>
>
> > I'm setting up a module that will grab a specific .xls and organize it
> > for production as well as load the information into Crystal for
> > archiving. =A0In the past, I have always directed the user to save
> > the .xls as "AnyFileName" in a specific folder and the module would
> > pick it up at a scheduled time. =A0This has me relying on the user to
> > not make any mistakes in naming the file.
>
> > Is there a produre in VBA that will open the most current file in a
> > specified folder?- Hide quoted text -
>
> - Show quoted text -

Thanks!  I didn't know where to start and this looks solid.
0
whicks (14)
6/27/2008 7:38:24 PM
Reply:

Similar Artilces:

encoding wmv file
hello I have a sample application developed in MFC dialog based project using directx sdk 9. the sample captures video from a webcam and saves it to a wmv file now i have to put that video into streaming server. i beleive that i will have to encode the wmv file using some codecs to put it into streaming server. i want to do the capturing and encoding simultaneously. below is a portion of my code to capture video // capture video pBuilder->RenderStream(&PIN_CATEGORY_CAPTURE, // Pin category &MEDIATYPE_Video, // Media type p...

VBA code to say Yes or No if any VBA code is present in ActiveWork
Using XL 2003 & 97 Would like hit Ctrl+Shift+V and have an answer either yes or no as to the presence of Any VBA code in the Active Workbook. I do know that about the VBA editor/Explorer or to press the Run Macro button and get a list from various sources . I would like to stay at the keyboard and process a series of macros including one to let me know if I even need to evoke the VBA explorer. TIA Dennis There is no bootstrap macro to tell you whether you have macros or not. If there were there would be no limit to virus writers. --- HTH, David McRitchie, Microsoft MVP - Excel ...

Outlook 2000 does not open messages with double click
Some messages will not open with double click but will open with FILE - OPEN. I'm not determine why the difference. Any thoughts? Thanks! ...

How can I deit a Borderart (.brd) file?
I am trying to change the background colour on the torn paper borderart with little to no success. I have placed this around a text box and provided a yellowed colour to the text box, but I am presented with a white gap before we get to the actual black of the torn border - this is sending me insane! I have searched the World Wide Wait to no avail, is there nobody else that is trying to do this? Select the border, copy, paste special as a .wmf (Windows Metafile), ungroup, click yes, ungroup again, delete everything but the border, you will have to adjust the corners. Select all, grou...

Print, Merged Cells, and VBA
Hello I'm looking for a code that will help me with a print job for a worksheet. There are a lot of merged cells and a lot of "IF"s to be used in the VBA for range selection. Thanks in advance, Sarr ...

Use VBA to update Access table or Query from Excel
Can I use VBA to update Access table or Query from Excel? Thanks in advance Hi Leungkong, > Can I use VBA to update Access table or Query from Excel? Of course, using ADO or DAO. See: http://www.erlandsendata.no/english/index.php?d=envbadacexportado Regards, Jan Karel Pieterse Excel MVP http://www.jkp-ads.com Hi Jan, Thanks. I think ADO is what I want. But I am not only want to export from excel to access. I want to edit some data in access table. For example, Access has a table "ProductList" I want to use Excel to call the product by "Pro...

Backup cannot copy .pst files
When I exit Outlook and click on Save Backup. I get the error "Cannot copy Outlook: The process cannot access the file beause another process has locked a portion of the file" Please advise with how to fix this problem. ...

Missing DLL file prevents me using PST Files?
Hello - I posted on here a while ago asking for help as following a company merger I cannot use PST files in Outlook 2000 (Windows NT machine). I have been told that work have removed a DLL file which is causing this. Do any of you have experience of this? If so any idea what DLL file I need to add back in? Many thanks, Carl. Do you mean that the corporate IT removed the DLL so you can't use PSTs? Do they have a good reason for this? "Carl Jarvis" <carljarvis@nothankyouforthespammister.co.uk> wrote in message news:1125040515.4863.0@nnrp-t71-03.news.uk.clara.ne...

New G5 tower has problem opening Word 2004
I have a brand new G5 running OSX 10.4.10. I have installed Office 2004, and am having trouble launching Word. After double clicking, it opens a blank window and then freezes as it works it way through the "optomizing font performance" bar at the bottom of the window. <br><br>It starts to give me a warning box for one font after another that says "This font is corrupt and should be removed." I believe, however, that Word itself it installing the fonts in Font Book just before it determines they are corrupt. Wierd. <br><br> It has taken me over 1/2 ho...

VBA and Scheduled task in Outlook
Hi, how can I: a) pull the query from somewhere and send it by e-mail automatically. E.g. I want to take a temperature table from http://www.wunderground.com/history/airport/EHAM/2009/7/10/DailyHistory.html?req_city=NA&req_state=NA&req_statename=NA where 2009/7/10 is the date of yesterday put it into an e-mail and send it automatically each day at 8 o'clock? b) if this is not possible, how can I send an e-mail with the attachment c:\Documents\query20090710.xls each day at 8 o'clock? my boss wants this automatized, I know how to write the VBA code for Excel, but don'...

increase adjustment cost (current) and decrease adjustment cost (F
We are in the process of getting ready to do our year end physical inventory count. We will be using the Stock Count functionality within GP and we have Multi-Bins enabled. We have a problem where we know that some significant inventory quantities are in the wrong BINs at the moment and will be corrected as part of the physical inventory count. I ran a test where I have an item that exists in multiple BIN locations but where the BIN quantities in GP were incorrect but the overall item quantity was correct. Example: GP has: Bin A = Qty 3 Bin B = Qty 4 Bin C = Qty 5 Total Qty = 12 Phy...

Opening and Closing Dialog Boxes
Hi, I'm developing a C++ Program using visual Studio 6. I have a problem with dialog boxes. I need to have Multiple Dialog boxes, and each one has different functions on it. For example, one has Contact Informaiton, another is the welcome screen. I only want to have one of these dialogs open at anyone time, so I need a way of Opening a new dialog box when a button is clicked, and when the new dialog box is open it becomes the main window and the other one is closed. I think I need to use modeless dialog boxes for this, but I'm not sure. I have tried everything I can think of....

vba select field
Hi, having a button on a form, is it possible to select only some fields (all from the same table) of the current record instead of all fields? I've tried this code: RunCommand acCmdSelectRecord RunCommand acCmdCopy Forse dipende dei dati nel record. but it selects ALL fields in the form. Thank you. Remigio hi, On 10.04.2010 18:01, remigio wrote: > having a button on a form, is it possible to select only some fields > (all from the same table) of the current record instead of all fields? > I've tried this code: > > RunCommand acCmdSelectRecord &...

Restoring Exchange Store files to a new server
We had a major system crash this week but I was able to grab the entire exchsrv directory from the server. What files do I need to copy to the new server to get all of my exchange mail/public folders/etc loaded on the new server? Thanks, CJ What version of Exchange? -- Ed Crowley MVP - Exchange "Protecting the world from PSTs and brick backups!" "Curious Joe" <joebob.johnson@gmail.com> wrote in message news:1160176822.798021.310710@m73g2000cwd.googlegroups.com... > We had a major system crash this week but I was able to grab the entire > exchsrv director...

VLOOKUP in VBA
On the worksheet I can insert in a cell =VLOOKUP(C5, Hobokee.xls!AcsLow, 2) and it works perfectly, looks up the value in column 2 of the range named AcsLow in the same workbook. But elswhere I want in a macro to lookup the same table and assign the result to a variable BalAmt. BalAmt = VLOOKUP(AccNum, Hobokee.xls!AcsLow, 2) does not work. It gives a function not defined error on Hobokee. If instead of Hobokee I put Workbooks("Hobokee"), it gives function not defined error for VLOOKUP which it changes to VLookup. I have been overVLOOKUP in the Help file and see nothing wrong. W...

xls file failure
A log.xls file was saved to the desktop in win'98se. On double clicking the file, an 'i' error is generated indicating that the 'file cannot be accessed. The file may be read only.'. The file attribute was archive and changing it to anything else, didn't help. Copy-paste and rename also didn't help. Is this file unrecoverable? Thanks. Len Delete all files in your C:\windows\temp directory -- files and directories that is, reboot and try again "lendapilot" <lendapilot@discussions.microsoft.com> wrote in message news:FE94FAF2-AF35-44B8-91F4-B97...

Check for File Open
Is there some way (in VBA) to check to see if a file (spreadsheet) is in use before you try to open it? -- Bill @ UAMS Ivan F Moala posted this (I didn't try it): Yes you can do this programtically. Have a look here for how to determine if a workbook is Open and by Who. http://www.xcelfiles.com/IsFileOpen.html and MS has a version: http://support.microsoft.com/default.aspx?scid=kb;EN-US;q138621 XL: Macro Code to Check Whether a File Is Already Open (Q138621) And it's even called "IsFileOpen" BillCPA wrote: > > Is there some way (in VBA) to check to see if ...

We can't use a public folder, we can see it?
I have created a Group mailing folder in 'All public folders', set the permissions for my colleagues. they can see the folder on the navigation page, but cannot see it in the list when they create a new email, why? We are using 2007. Is this a Contacts folder? You'll have to add it to your Public Folder Favorites first. See http://www.msoutlook.info/question/292 After that, you can configure the properties of the Contacts folder to show as an Outlook Address Book. -- Robert Sparnaaij [MVP-Outlook] Coauthor, Configuring Microsoft Outlook 2003 http://www.howto...

Close One Open Another
From a form I want to close a split database and open another without exiting access. How can I simply do this... DoCmd.Close acForm,"frmDone" DoCmd.Close Front.mdb DoCmd.Close Back.mdb DoCmd.Open db, New.mdb Thanks, DS DS wrote: > From a form I want to close a split database and open another without > exiting access. How can I simply do this... > > DoCmd.Close acForm,"frmDone" > DoCmd.Close Front.mdb > DoCmd.Close Back.mdb > DoCmd.Open db, New.mdb > > Thanks, > DS I tried this but it closes the mdb and access and doesn't reopen...

Unable to Open received PO (change to "change order")
Great Plains will not allow me to change this Purchase Order from Received to Change Order: Here is the message I get: However, if I try to enter a quantity cancelled, I get: How can I re-open this Purchase Order so I can change the price??? Thank you. Unfortunately, the pasted messages did not come through. Once a line item is received, you can change its cost only during invoice matching. The process would be to return the item; add a new row with the correct cost; and receive and invoice the new item. Not an elegant solution, for sure. -- Charles Allen, MVP &quo...

Turn pc speaker on/off with vba code
Is this possible? Thanks I found something here on how to do it withVBScript. You can probably modify it to work in VBA http://www.pcreview.co.uk/forums/thread-1468591.php -- HTH, Barb Reinhardt "David" wrote: > Is this possible? > Thanks Thanks Barb I'll have a go (I need to improve my web searching skills) "Barb Reinhardt" wrote: > I found something here on how to do it withVBScript. You can probably modify > it to work in VBA > > http://www.pcreview.co.uk/forums/thread-1468591.php > -- > HTH, > ...

Excel VBA/Macro programming for beginners
Hi all, I know a bit VB 6.0 programming. Will it help me to write exce vba/macro(same thing?).can anybody help me by providing some fre websites where i can have informations/codes for VBA/macro. Please tell me how could i make a *command Button * in a cell in th excel sheet -- sdebu_200 ----------------------------------------------------------------------- sdebu_2000's Profile: http://www.officehelp.in/member.php?userid=430 View this thread: http://www.officehelp.in/showthread.php?t=118812 Posted from - http://www.officehelp.i Try David McRitchie's "getting started with mac...

Printing a merged to file
Hello, I have searched for this topic but did not find another similar question. I am merging mail addresses to a four up postcard form. If I merge to a document and try to print it, I either get four copies of the same address on each sheet or one copy in the middle of the four up form. If I merge to the printer, I get four addresses per sheet in the right place. That would be fine, but some of the addresses are long enough that they wrap and they wrap in a place where the program decides. Foe example: Johnathan Quixley & Jane Elizabeth Doe may wrap to be "Johnathan Quixley & J...

File> ... Can I see more file names?
When I click on File I see (below the list of available options) four previously saved xls files. How do I see more , say the last 10 saved files? <Tools> <Options> <General> tab, And increase to 9 (max) the number of entries in the "Recently Used File box. -- HTH, RD ============================================== Please keep all correspondence within the Group, so all may benefit! ============================================== "SouthAfricanStan" <me@there> wrote in message news:uAdxTx8FFHA.1260@TK2MSFTNGP12.phx.gbl... When I click on File I see...

Loading a Form from VBA
Hi all, I'd like a module to open and load a form in my Access 2003 database... How can I do this? Use the OpenForm method. Look in the Help file for details. -- Doug Steele, Microsoft Access MVP http://I.Am/DougSteele (no e-mails, please!) "Mike" <michael.leon.schwartz@gmail.com> wrote in message news:1184770076.109526.207900@o11g2000prd.googlegroups.com... > Hi all, I'd like a module to open and load a form in my Access 2003 > database... How can I do this? > On Jul 18, 10:58 am, "Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com&g...