简体   繁体   English

使用VBA脚本/计划任务制作每周文件夹

[英]Make a Weekly Folder With VBA Script/Scheduled Task

I'm trying to make an automatic folder generation process per week. 我正在尝试每周进行一次自动文件夹生成过程。 We have timesheets coming in and we have to put them into an organised folder structure (it's on a shared drive, so for example: Y:\\Timesheets\\2013-09-22" (for timesheets for the week ending the 22nd of September)). That folder will then have folders relating to the employee names. 我们有时间表,我们必须将它们放入一个有组织的文件夹结构中(位于共享驱动器上,因此,例如:Y:\\ Timesheets \\ 2013-09-22“(表示9月22日结束的一周的时间表) )。该文件夹将包含与员工姓名相关的文件夹。

We use an excel tick sheet which is up to date all of the time with the latest information to make sure that we have received the timesheets. 我们使用的Excel刻度表始终保持最新,并提供最新信息,以确保我们已收到时间表。 I was thinking that I could make an automatic folder generation process per week, rather than copying and pasting every empty folder from next week to the week after (as it transfers a lot of old folders which are simply not used). 我当时以为我可以每周进行一次自动的文件夹生成过程,而不是从下周到下周再复制和粘贴每个空文件夹(因为它会转移很多根本不使用的旧文件夹)。

So far I can create folders with this: 到目前为止,我可以使用以下方法创建文件夹:

Sub CreateDirs()

    createDirsFromRange "Z:\Timesheets", "2013-10-13", Range("B2:B182")

End Sub

My only questions are: 我唯一的问题是:

  1. Can I make the date part of that code get the date from the PC (and therefore run it every Sunday) to make sure we have the correct title for each folder? 我可以使代码中的日期部分从PC上获取日期(并因此在每个星期日运行该日期),以确保每个文件夹的标题正确吗?

  2. Could I run a batch file to open the Excel spreadsheet, run the VBA code and then close it again? 我可以运行批处理文件来打开Excel电子表格,运行VBA代码,然后再次将其关闭吗? If so, what would the coding for that be? 如果是这样,那将是什么编码?

  1. Use the Date() function (see this link for more options) 使用Date()函数(有关更多选项,请参见此链接

  2. A solution could be : 一个解决方案可能是:

In your batch, just run excel giving it your workbook filename as parameter : 在您的批处理中,只需运行excel,并为其提供您的工作簿文件名作为参数:

"C:\Program Files\...\excel.exe" "C:\...\myworkbook.xls"

Then, you can use the Workbook.Open event and the Workbook.Close function so that your macro runs everytime the workbook is opened (so you just have to open it with your batch file). 然后,您可以使用Workbook.Open事件和Workbook.Close函数,以便您的宏在每次打开工作簿时都运行(因此您只需使用批处理文件打开它)。

Private Sub Workbook_Open() 
    ' Call your macro here
    ThisWorkbook.Close()
End Sub

But this means the macro will run everytime so you won't be able to open the workbook without having the macro run and the workbook automaticaly closed... 但这意味着宏每次都会运行,因此如果不运行宏并且工作簿自动关闭,您将无法打开工作簿...

So you may consider using a specific command line argument to trigger the macro execution (see this link ) 因此,您可以考虑使用特定的命令行参数来触发宏执行(请参阅此链接

As far as 1) is concerned, change the date string to Date$ , which will give you the current date in an acceptable format (mm-dd-yyyy). 就1)而言,将日期字符串更改为Date$ ,它将以可接受的格式(mm-dd-yyyy)为您提供当前日期。

As far as 2) is concerned, instead of trying to automate that part of it, why not just run the code on Friday at the end of business or sometime on Friday while the file is open? 就2)而言,为什么不尝试自动执行部分代码,而不是在业务结束时在星期五或打开文件时在星期五的某个时间运行代码? You could use this code for your date (to show the date for Sunday instead of Friday): mid(date$,1,3) & mid(date$,4,2)+2 & mid(date$,6,5) . 您可以使用此代码作为日期(以显示星期日而不是星期五的日期): mid(date$,1,3) & mid(date$,4,2)+2 & mid(date$,6,5)

I'm assuming you're in a Windows environment. 我假设您在Windows环境中。 If so, why not just use Windows Task Scheduler? 如果是这样,为什么不只使用Windows Task Scheduler? It's built in, and you can tell it to kick off your Excel sheet (or a BAT file) every Sunday or whatever day of the week you want it to. 它是内置的,您可以告诉它在每个星期日或您希望星期几的任何一天开始Excel工作表(或BAT文件)。

Here's a link to the Windows 7 version. 这是Windows 7版本的链接。 And another for the Windows XP version. 另一个用于Windows XP版本。

Here's a link to a really good page on how to extract the current date using a BAT file . 这是一个非常好的页面的链接,该页面介绍了如何使用BAT文件提取当前日期 You can probably figure out how to edit it to suit your needs. 您可能会想出如何编辑它以满足您的需求。

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

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