gpt4 book ai didi

excel - 在文件夹中循环并删除 .xlsm 文件

转载 作者:行者123 更新时间:2023-12-04 20:04:44 30 4
gpt4 key购买 nike

亲爱的,

我创建了一个代码来循环遍历文件夹并删除 10 年前的文件,但循环无法正常工作,因为 FolderPath(变量)保持不变并且循环遵循无限循环。有帮助吗?

Sub LoopThroughFolder()

Dim FolderPath As String

FolderPath = Dir("C:\Users\XXXX\XXXX\XXXX\XXXX\XXXX\*.xlsm")

Do While FolderPath <> ""
If DateValue( _
Mid(FolderPath, InStr(FolderPath, ".") + 1, 2) & "/" & _
Mid(FolderPath, InStr(FolderPath, ".") + 3, 2) & "/" & _
Mid(FolderPath, InStr(FolderPath, ".") + 5, 4)) _
< DateValue(Date - 10) Then

Application.DisplayAlerts = False
SetAttr "C:\Users\XXXX\XXXX\XXXX\XXXX\XXXX\" & FolderPath, vbNormal
Kill "C:\Users\XXXX\XXXX\XXXX\XXXX\XXXX\" & FolderPath
Application.DisplayAlerts = True

FolderPath = Dir

End If
Loop

End Sub

最佳答案

你需要搬家FolderPath = Dir ,
目前它位于 IF 语句中,这意味着如果当前文件早于 10 天,它只会查找下一个文件。

Sub LoopThroughFolder()

Dim FolderPath As String

FolderPath = Dir("C:\Users\XXXX\XXXX\XXXX\XXXX\XXXX\*.xlsm")

Do While FolderPath <> ""
If DateValue( _
Mid(FolderPath, InStr(FolderPath, ".") + 1, 2) & "/" & _
Mid(FolderPath, InStr(FolderPath, ".") + 3, 2) & "/" & _
Mid(FolderPath, InStr(FolderPath, ".") + 5, 4)) _
< DateValue(Date - 10) Then

Application.DisplayAlerts = False
SetAttr "C:\Users\XXXX\XXXX\XXXX\XXXX\XXXX\" & FolderPath, vbNormal
Kill "C:\Users\XXXX\XXXX\XXXX\XXXX\XXXX\" & FolderPath
Application.DisplayAlerts = True
End If
FolderPath = Dir
Loop

End Sub

关于excel - 在文件夹中循环并删除 .xlsm 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52780312/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com