简体   繁体   English

Excel:宏以检查文件夹中是否有新文件(基于单元格中的日期)

[英]Excel: Macro to check folder for new files (based on date in a cell)

I have a macro that needs ran once a day. 我有一个宏,每天需要运行一次。 It needs to check a folder (this can be specified within the VBA, as it should not change) and then see if there are any new files. 它需要检查一个文件夹(可以在VBA中指定该文件夹,因为它不应更改),然后查看是否有任何新文件。

Currently, this Excel VBA Program prompts the user to select a file. 当前,此Excel VBA程序提示用户选择文件。 It then does things to the file, and saves the date and shift (from the filename string) into cells on sheet one. 然后,它对文件进行处理,并将日期和日期(从文件名字符串中移出)保存到第一页的单元格中。 The string is always the same, with the only variation being the date and shift number: "My Information MM-DD-YY # Shift" 字符串始终相同,唯一的变化是日期和班次编号:“我的信息MM-DD-YY#班次”

I want to create code so that the user does not select new files each day, but the program checks for them via a comparison of the cells with the last date and shift ran to the date and shift within the file name in the folder. 我想创建代码,以使用户不必每天都选择新文件,但是程序会通过将单元格与上次日期进行比较来检查它们,然后将日期移动到文件夹中文件名之内。

Example: 例:

The user ran a file yesterday and it saved the date and shift into cells A1 and B1, 10-04-17 and 2, respectively. 用户昨天运行了一个文件,它保存了日期并将其转移到单元格A1和B1、10-04-17和2中。

The user forgot to run shift 3 yesterday. 该用户昨天忘记执行变速3。

They click the run button today, and the code looks at the directory (specified in the VBA) to see if there are any files where the date is equal to the date in cell A1 (10-04-17) and greater than the shift in cell B1 (2) as well as if there are any files greater than the date in shift A1 (10-04-17) with any shift number. 他们今天单击了运行按钮,并且代码查看了目录(在VBA中指定),以查看是否存在日期等于单元格A1(10-04-17)中的日期且大于shift的文件。单元格B1(2)中的文件,以及是否有大于班次A1(10-04-17)中日期的文件且带有任何班次编号。 Once it checks, it will select any of the files that meet this criteria and run the macro on them, saving the most recent date and shift to cells A1 and B1. 一旦检查,它将选择任何符合此条件的文件并在其上运行宏,保存最新日期并移至单元格A1和B1。

I already have multifile run functionality written into the existing program. 我已经将多文件运行功能写入现有程序。

Any help is appreciated! 任何帮助表示赞赏! Thank you in advance! 先感谢您!

For anyone who finds this and is curious, 对于任何发现并好奇的人,

I solved this in a bit of a roundabout way: 我以一种回旋的方式解决了这个问题:

  1. Create sheet (will stay hidden by turning on and off the visibility) 创建工作表(通过打开和关闭可见性将保持隐藏状态)
  2. Use filedatetime to view the file I last ran (it's name is saved on the home page (sheet 1) of my workbook) 使用filedatetime查看我上次运行的文件(其名称保存在工作簿的首页(第1页)中)
  3. Pulls all the files from a specific folder and prints their filedatetime 从特定文件夹中提取所有文件并打印其文件日期时间
  4. Sees which filedatetimes are greater than that of the last file I ran 查看哪个文件日期时间大于我运行的上一个文件的时间
  5. For each one found, it gets set as a variable (using a loop) that the macro runs on it 对于找到的每个变量,将其设置为变量(使用循环),宏在该变量上运行
  6. Finds the greatest filedatetime and sets this filename to the homepage 查找最大的文件日期时间并将此文件名设置为主页
  7. Deletes all the data from the sheet and hides it 删除工作表中的所有数据并将其隐藏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM