【发布时间】:2015-06-08 07:04:47
【问题描述】:
我在 excel 上运行以下宏以将特定文件夹中的文件列表创建为 Auto_Open
Option Explicit
Sub Auto_Open()
Dim MyFolder As String
Dim MyFile As String
Dim NextRow As Long
Dim MyDateTime As Date
Dim MyDate As Date
MyFolder = "C:\Users\Folder\"
MyFile = Dir(MyFolder)
NextRow = 1
Do While Len(MyFile) > 0
MyDateTime = FileDateTime(MyFolder & MyFile)
MyDate = Int(MyDateTime)
If MyDate = Date Then
Cells(NextRow, "A").Value = MyFolder & MyFile
Cells(NextRow, "B").Value = MyDateTime
NextRow = NextRow + 1
End If
MyFile = Dir
Loop
Dim wb As Workbook
Set wb = Workbooks.Add
ThisWorkbook.Sheets("Sheet1").Copy Before:=wb.Sheets(1)
wb.SaveAs ThisWorkbook.Path & "\" & Format(Now, "yyyy-mm-dd") & "_FileList", FileFormat:=51
wb.Close
ThisWorkbook.Close SaveChanges:=False
End Sub
我计划每天通过 Windows 任务计划程序运行包含此宏的 excel 文件。它按照程序创建一个 excel 文件并关闭除 excel 应用程序之外的所有工作簿。但是,之后我收到“Microsoft Excel 已停止工作”的错误。
为什么会这样?有关如何解决此问题的任何建议?
【问题讨论】:
-
Sheet1上有什么 - 任何按钮或控件?为什么不在代码的前面创建新工作簿并将值直接写入其中? -
工作表 1 为空。我可以尝试这样做
标签: vba excel scheduled-tasks