我需要反转这个循环:
我尝试过使用"Step -1",但不起作用。我猜这是因为这是一种循环的FSO方法。
Set d = fso.GetFolder("address")
For Each File In d.files
If (Int(Mid(File.Name, 30, 5)) = Int(nfprocurada)) Then
ActiveWorkbook.FollowHyperlink Address:=File.Path, ExtraInfo:=False
Exit Sub
End If
Next发布于 2021-07-17 03:31:52
For Each循环不接受步长或符号的任何更改。因此,如果您想反转方向,则需要将循环类型更改为像For counter = start to end这样的index based loop。由于Files collection不接受索引作为论据,因此您需要将每个文件复制到一个数组中,然后才能使用索引遍历文件。
试试这个:
Set d = FSO.getfolder("address")
Dim AllFiles() As Object, FileCount As Integer
For Each File In d.Files
ReDim Preserve AllFiles(FileCount)
Set AllFiles(UBound(AllFiles)) = File
FileCount = FileCount + 1
Next File
Dim i as Long
For i = UBound(AllFiles) To LBound(AllFiles) Step -1
If (Int(Mid(File.Name, 30, 5)) = Int(nfprocurada)) Then
ActiveWorkbook.FollowHyperlink Address:=AllFiles(i).Path, ExtraInfo:=False
Exit Sub
End If
Next ihttps://stackoverflow.com/questions/68413847
复制相似问题