如何使 excel 备份检查器脚本获取最新日期的文件
How to make excel backup checker script get file with newest date
我有一个程序可以在格式化后创建备份:<name>_<date>
有些备份较多,有些备份none。
最多保留 3 个名称相同但日期不同的备份。
我有一个 excel table,其中包含名称和一个脚本,用于检查是否存在备份,并在结果后为单元格着色。
我想改进脚本,让它检查备份是否过时(超过 30 天)。
我有想法了,但是我运行遇到了问题。如果有多个同名备份,我不知道如何比较最新的备份。
脚本:
'auto check if backup exists - on click
file = Dir("<filepath>")
Do While file <> ""
myBool = False
backup = Right(backup, 6)
If InStr(file, backup) > 0 Then
If now - FileDateTime(file) > 30 Then 'this is where I have the problem... how do I make it grab the newest of the backups?
'outdated
End If
myBool = True
ActiveCell.Interior.ColorIndex = "35"
Exit Do
End If
file = Dir
Loop
If Not myBool Then
ActiveCell.Interior.ColorIndex = "22"
End If
我使用 Dir()
函数找到了绕过它的方法:
'auto check if backup exists - on click
Dim i
i = 1
file = Dir(path)
Do While file <> ""
myBool = False
isnew = False
backup = Right(backup, 6)
If InStr(file, backup) > 0 Then
myBool = True
cfile = Dir(path & Left(c, 7) & "*") 'c is the active cell
Do While cfile <> ""
ReDim arr(i)
arr(i) = FileDateTime(path & cfile)
If Now - arr(i) < 30 Then
isnew = True
End If
i = i + 1
cfile = Dir()
Loop
If isnew = False Then
ActiveCell.Interior.ColorIndex = "6"
ElseIf isnew = True Then
ActiveCell.Interior.ColorIndex = "35"
End If
Exit Do
End If
file = Dir
Loop
If Not myBool Then
ActiveCell.Interior.ColorIndex = "22"
End If
我有一个程序可以在格式化后创建备份:<name>_<date>
有些备份较多,有些备份none。
最多保留 3 个名称相同但日期不同的备份。
我有一个 excel table,其中包含名称和一个脚本,用于检查是否存在备份,并在结果后为单元格着色。
我想改进脚本,让它检查备份是否过时(超过 30 天)。
我有想法了,但是我运行遇到了问题。如果有多个同名备份,我不知道如何比较最新的备份。
脚本:
'auto check if backup exists - on click
file = Dir("<filepath>")
Do While file <> ""
myBool = False
backup = Right(backup, 6)
If InStr(file, backup) > 0 Then
If now - FileDateTime(file) > 30 Then 'this is where I have the problem... how do I make it grab the newest of the backups?
'outdated
End If
myBool = True
ActiveCell.Interior.ColorIndex = "35"
Exit Do
End If
file = Dir
Loop
If Not myBool Then
ActiveCell.Interior.ColorIndex = "22"
End If
我使用 Dir()
函数找到了绕过它的方法:
'auto check if backup exists - on click
Dim i
i = 1
file = Dir(path)
Do While file <> ""
myBool = False
isnew = False
backup = Right(backup, 6)
If InStr(file, backup) > 0 Then
myBool = True
cfile = Dir(path & Left(c, 7) & "*") 'c is the active cell
Do While cfile <> ""
ReDim arr(i)
arr(i) = FileDateTime(path & cfile)
If Now - arr(i) < 30 Then
isnew = True
End If
i = i + 1
cfile = Dir()
Loop
If isnew = False Then
ActiveCell.Interior.ColorIndex = "6"
ElseIf isnew = True Then
ActiveCell.Interior.ColorIndex = "35"
End If
Exit Do
End If
file = Dir
Loop
If Not myBool Then
ActiveCell.Interior.ColorIndex = "22"
End If