无法将“ fileobj”名称从循环复制到电子表格范围

时间:2019-04-22 01:58:11

标签: excel range filenames

我在USB记忆棒上设置了文件的文件对象,并希望将其名称复制到工作表中的excel范围内,直到您尝试将fileObj.Name保存到一个范围内时,它才起作用。

大多数代码来自这个绅士5年前对这个问题的出色回答。“麻烦代码读取循环读取多个文件并导入特殊行”,他提供了两个很好的答案。并且大多数情况下都有效,除了类型不匹配或对象未设置错误(取决于您所更改的内容)之外。

    Option Explicit
    Sub Test1()

      Dim FileSysObj As Object
      Dim FolderObj As Object
      Dim FileColl As Object
      Dim FileObj As Object
      Dim s, fc, f1, CurrFile, cfile
      Dim PathSearch As String
      Dim FinalRow As Range


      ' This assumes the text files are in the same folder as the workbook holding the macro.
      ' Use PathSearch = "C:\ ..." if you would rather have the files in a different folder.
      PathSearch = ActiveWorkbook.Path

      Set FileSysObj = CreateObject("Scripting.FileSystemObject")
      Set FolderObj = FileSysObj.getfolder(PathSearch)
      Set FileColl = FolderObj.subfolders


     ' For Each FileObj In FileColl


           ' Set CurrFile = FileObj.Files

          '  For Each cfile In CurrFile
           '  s = s & cfile.Name

       '    ***FinalRow = Cells(Rows.Count).End(xlUp).Row.Value***

    ' Ans = FinalRow.Value = s
     "Here is the solution code

           For Each FileObj In FileColl


        Set currfile = FileObj.Files

        For Each cfile In currfile
         i = i + 1
         f = cfile.Name

      'Now deal with your worksheets

        Cells(i, 1).Value = f


      Next

  Next

End Sub





I have added the solution. below was the problem, no one replied.

我遇到类型不匹配错误或集合错误或未设置对象错误的情况,我希望在电子表格的USB文件夹中获取文件名,以保存USB内容的数据库。

0 个答案:

没有答案