Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 永远的循环_Excel_Vba - Fatal编程技术网

Excel 永远的循环

Excel 永远的循环,excel,vba,Excel,Vba,在第一次通话中,它就工作了,并且做了它应该做的事情 但在第二次通话中,它陷入了这个循环: something (tecan1) something (tecan2) Do While Dir“” 雷迪姆保留阿菲尔斯塔雷(乌邦(阿菲尔斯塔雷)+1) aFirstArray(UBound(aFirstArray))=Mid(Dir,1,4) 环 为什么它会被卡在循环中?我会避免使用Dir函数,因为您试图这样做。每次调用它时,它都会返回下一个文件名。但不知道为什么循环会卡住 我会使用FileSys

在第一次通话中,它就工作了,并且做了它应该做的事情

但在第二次通话中,它陷入了这个循环:

something (tecan1)
something (tecan2)
Do While Dir“”
雷迪姆保留阿菲尔斯塔雷(乌邦(阿菲尔斯塔雷)+1)
aFirstArray(UBound(aFirstArray))=Mid(Dir,1,4)
环

为什么它会被卡在循环中?

我会避免使用Dir函数,因为您试图这样做。每次调用它时,它都会返回下一个文件名。但不知道为什么循环会卡住

我会使用FileSystemObject类,它对您有更大的控制。以下是一个例子:

Do While Dir <> ""
    ReDim Preserve aFirstArray(UBound(aFirstArray) + 1)
    aFirstArray(UBound(aFirstArray)) = Mid(Dir, 1, 4)
Loop

我会避免使用Dir函数,因为您试图这么做。每次调用它时,它都会返回下一个文件名。但不知道为什么循环会卡住

我会使用FileSystemObject类,它对您有更大的控制。以下是一个例子:

Do While Dir <> ""
    ReDim Preserve aFirstArray(UBound(aFirstArray) + 1)
    aFirstArray(UBound(aFirstArray)) = Mid(Dir, 1, 4)
Loop

每次使用Dir时,迭代器都会移动(即使在Dir上有手表,也会发生这种情况)

用下面的代码替换你的循环

Function GetFiles(fileParam As String) As Collection
'create reference to Microsoft Scripting Runtime
'scrrun.dll

    Const dir               As String = "C:\"
    Dim fso                 As New FileSystemObject
    Dim myFolder            As Folder
    Dim loopFile            As File
    Dim returnCollection    As New Collection

    Set myFolder = fso.GetFolder(dir)

    For Each loopFile In myFolder.Files
        If loopFile.Name Like fileParam & "*.ESY" Then
            'add the loopfile path into the collection
            returnCollection.Add loopFile.Path
        End If
    Next loopFile

    Set GetFiles = returnCollection
End Function
f=Dir
在f“”时执行
雷迪姆保留阿菲尔斯塔雷(乌邦(阿菲尔斯塔雷)+1)
Afirstaray(UBound(aFirstArray))=中间(f,1,4)
f=Dir
环
您的代码循环是因为

  • 在命中“”后再次调用Dir(返回无效的过程调用或参数)
  • 您有一个奇数(>1)个*.ESY文件
  • 下一步恢复时出现错误

  • 每次使用Dir时,迭代器都会移动(即使在Dir上有手表,也会发生这种情况)

    用下面的代码替换你的循环

    Function GetFiles(fileParam As String) As Collection
    'create reference to Microsoft Scripting Runtime
    'scrrun.dll
    
        Const dir               As String = "C:\"
        Dim fso                 As New FileSystemObject
        Dim myFolder            As Folder
        Dim loopFile            As File
        Dim returnCollection    As New Collection
    
        Set myFolder = fso.GetFolder(dir)
    
        For Each loopFile In myFolder.Files
            If loopFile.Name Like fileParam & "*.ESY" Then
                'add the loopfile path into the collection
                returnCollection.Add loopFile.Path
            End If
        Next loopFile
    
        Set GetFiles = returnCollection
    End Function
    
    f=Dir
    在f“”时执行
    雷迪姆保留阿菲尔斯塔雷(乌邦(阿菲尔斯塔雷)+1)
    Afirstaray(UBound(aFirstArray))=中间(f,1,4)
    f=Dir
    环
    
    您的代码循环是因为

  • 在命中“”后再次调用Dir(返回无效的过程调用或参数)
  • 您有一个奇数(>1)个*.ESY文件
  • 下一步恢复时出现错误

  • @|_:tecan1和tecan2是唯一可以传入的两个参数,还是您可以执行tecan3、tecan4……等等?@|:tecan1和tecan2是唯一可以传入的两个参数,还是您可以执行tecan3、tecan4、,…等等?fink这确实是一个很好的主意,但我不想重新开始,所以你能用我的代码提供解决方案fink这确实是一个很好的主意,但我不想重新开始,所以你能用我的代码提供解决方案吗