如果今天修改了txt文件,则Excel VBA退出子

时间:2019-10-22 10:59:15

标签: excel vba

如果.txt文件今天(即过去30 s内)被修改,我想停止宏,下面是我的代码,但是每当我尝试时它在行file = ThisWorkbook.Path & "\Logs.txt\"时给出错误91运行它。我想念什么?谢谢。

Sub Calculate()

Dim Fdate As Date
Dim FileInFromFolder As Object
Dim file As Object

file = ThisWorkbook.Path & "\Logs.txt\"

Set FSO = CreateObject("scripting.filesystemobject")

Fdate = file.Int(FileInFromFolder.DateLastModified)

If Fdate = Date Then GoTo eh
Else

'Minimize workbook
ActiveWindow.WindowState = xlMinimized

'Switch to manual calculation of formulae
Application.Calculation = xlManual
ActiveWorkbook.PrecisionAsDisplayed = False
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False

Call Backup

Call Move

'Switch to automatic calculation of formulae
Application.Calculation = xlAutomatic
ActiveWorkbook.PrecisionAsDisplayed = True
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True

Done:
    Exit Sub
eh:
    ' All errors will jump to here
    MsgBox "error test"
End Sub

编辑:添加了错误位置。

2 个答案:

答案 0 :(得分:5)

首先,您要说的是变量文件的类型为Object

Dim file As Object

然后为它分配一个字符串

file = ThisWorkbook.Path & "\Logs.txt\"

哪个无效。

我猜您正在尝试从路径中获取文件对象。

您可以使用创建(但未使用)的FileSystemObject来做到这一点

dim fileName as String
Dim file As Object
Dim FSO as Object

fileName = ThisWorkbook.Path & "\Logs.txt"
set FSO = CreateObject("scripting.filesystemobject")
set file = FSO.GetFile(fileName)

答案 1 :(得分:-1)

问题是\中的尾随file = ThisWorkbook.Path & "\Logs.txt\"