无效的过程调用或Debug.Print上的参数

时间:2019-07-03 14:31:08

标签: excel vba

尝试使用几天前在这里收到的一些建议来重做几天前我做的函数...在Debug.Print的行上收到无效的过程调用或参数。

VBA和Excel的新功能。感谢您的帮助和耐心。

Public Sub GetSOPFiles()
    ' Set folder path
    Const FolderPath As String = "C:\Users\test\Desktop\SOP Excel Prototype"

    Const FileExt As String = "docx"

    Dim Result As Variant
    Dim i As Integer
    Dim MyFile As Object
    Dim MyFSO As Object
    Dim MyFolder As Object
    Dim MyFiles As Object
    Dim dept As Variant
    Dim deptCodes As Variant

    Set MyFSO = CreateObject("Scripting.FileSystemObject")
    Set MyFolder = MyFSO.GetFolder(FolderPath)
    Set MyFiles = MyFolder.Files

    Dim vData As Variant
    Dim sTemp As Variant

    ReDim vData(0 To MyFiles.Count - 1, 0 To 0)

    x = 1

    With MyFiles
        While x <= UBound(vData, 1)
            Debug.Print (MyFiles(x).Name)
        Wend
    End With
End Sub

1 个答案:

答案 0 :(得分:0)

MyFiles是一个集合,应使用For Each...Next循环对其进行迭代。

Set MyFSO = CreateObject("Scripting.FileSystemObject")
Set MyFolder = MyFSO.GetFolder(FolderPath)
Set MyFiles = MyFolder.Files

For Each MyFile In MyFiles
    Debug.Print MyFile.Name ' no parentheses
Next

您无法索引到MyFiles集合中。按照Files documentation的说明使用For Each...Next循环。