【问题标题】:Listing all folders in my directory visual basic列出我的目录 Visual Basic 中的所有文件夹
【发布时间】:2016-08-30 19:15:18
【问题描述】:

我正在尝试通过按一下按钮将目录中驱动器中的所有文件夹列出到 Excel 电子表格中。我制作了按钮并分配了这个宏......为什么它不能编译? *** **** 显示了他们调试的内容。所述对象文件夹不是对象。请帮忙!

Sub ListAllFile()

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim ws As Worksheet

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ws = Worksheets.Add

 'Get the folder object associated with the directory

***Set objFolder = fso.GetFolder("C:hello\EMILY")***
ws.Cells(1, 1).Value = objFolder.Name

 'Loop through the Files collection
For Each objFile In objFolder.Files
    ws.Cells(ws.UsedRange.Rows.Count + 1, 1).Value = objFile.Name
Next


End Sub

【问题讨论】:

  • c:hello 是相对路径,位于 C: 驱动器上。任何导演最后cd'd 到 C: 驱动器将用作该路径的“基础”。也许你的意思是c:\hello? (注意反斜杠)。
  • 那不是VB.NET代码,VB.NET不做宏。我怀疑你打算使用 excel-vba 标签
  • @MarcB 我试过了。还是什么都没有。
  • @Plutonix 代码是什么?
  • 错误是你写的吗?或者是错误“需要对象”。如果是后者,在您的错误行中,您使用 fso 作为对象,但您从未将 fso 设置为任何内容。如果您在 VBA 中将选项设置为要求变量声明,您会发现这一点,这会将 Option Explicit 放在开头。试试看,你就会明白我的意思。该错误消息是因为您从未将 fso 声明为对象。

标签: excel vba directory


【解决方案1】:

这将允许您获取文件夹名称,除非您确实需要文件。它是根据您的原始代码修改的。我注释掉了 excel/工作表的逻辑。

问题的一部分是 fso.GetFolder 不是声明和设置的对象。如果您仍然想要文件,可以将 objFolder.Subfolders 更改为 .Files

Sub ListAllFile()

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim ws As Worksheet

Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set ws = Worksheets.Add

 'Get the folder object associated with the directory

Set objFolder = objFSO.GetFolder("C:\users")
'ws.Cells(1, 1).Value = objFolder.Name

'Loop through the Files collection
For Each objFile In objFolder.subfolders
 MsgBox objFile.Name ' to test output
'ws.Cells(ws.UsedRange.Rows.Count + 1, 1).Value = objFile.Name
Next


End Sub

【讨论】:

    【解决方案2】:

    有很多方法可以做到这一点。这是一种方法。

    Option Explicit
    Sub FileListingAllFolder()
    
    Dim pPath As String
    Dim FlNm As Variant
    Dim ListFNm As New Collection ' create a collection of filenames
    
    Dim OWb As Workbook
    Dim ShtCnt As Integer
    Dim Sht As Integer
    
    Dim MWb As Workbook
    Dim MWs As Worksheet
    Dim i As Integer
    
    ' Open folder selection
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        pPath = .SelectedItems(1)
    End With
    
    Application.WindowState = xlMinimized
    Application.ScreenUpdating = False
    
    ' Create master workbook with single sheets
    Set MWb = Workbooks.Add(1)
    MWb.Sheets(1).Name = "Result"
    Set MWs = MWb.Sheets("Result")
    Cells(1, 1) = "No."
    Cells(1, 2) = "Sheet Name"
    Cells(1, 3) = "File Name"
    Cells(1, 4) = "Link"
    i = 2
    
    ' Filling a collection of filenames (search Excel files including subdirectories)
    Call FlSrch(ListFNm, pPath, "*.xls", True)
    
    ' Print list to immediate debug window and as a message window
    For Each FlNm In ListFNm ' cycle for list(collection) processing
    
        'Start Processing here
        Set OWb = Workbooks.Open(FlNm)
        ShtCnt = ActiveWorkbook.Sheets.Count
        For Sht = 1 To ShtCnt
            MWs.Cells(i, 1) = i - 1
            MWs.Cells(i, 2) = Sheets(Sht).Name
            MWs.Cells(i, 3) = OWb.Name
            MWs.Cells(i, 4).Formula = "=HYPERLINK(""" & FlNm & """,""Click Here"")"
            i = i + 1
        Next Sht
        'End file processing file
        OWb.Close False
    Next FlNm
    
    ' Print to immediate debug window and message if no file was found
    If ListFNm.Count = 0 Then
        Debug.Print "No file was found !"
        MsgBox "No file was found !"
        MWb.Close False
        End
    End If
    
    MWb.Activate
    MWs.Activate
    Cells.Select
    Selection.EntireColumn.AutoFit
    Range("A1").Select
    Application.ScreenUpdating = True
    Application.WindowState = xlMaximized
    
    End
    
    NextCode:
    MsgBox "You Click Cancel, and no folder selected!"
    
    End Sub
    
    Private Sub FlSrch(pFnd As Collection, pPath As String, pMask As String, pSbDir As Boolean)
    
    Dim flDir As String
    Dim CldItm As Variant
    Dim sCldItm As New Collection
    
    ' Add backslash at the end of path if not present
    pPath = Trim(pPath)
    If Right(pPath, 1) <> "\" Then pPath = pPath & "\"
    
    ' Searching files accordant with mask
    flDir = Dir(pPath & pMask)
        Do While flDir <> ""
            pFnd.Add pPath & flDir 'add file name to list(collection)
            flDir = Dir ' next file
        Loop
    
    ' Procedure exiting if searching in subdirectories isn't enabled
    If Not pSbDir Then Exit Sub
    
    ' Searching for subdirectories in path
    flDir = Dir(pPath & "*", vbDirectory)
        Do While flDir <> ""
    
            ' Add subdirectory to local list(collection) of subdirectories in path
            If flDir <> "." And flDir <> ".." Then If ((GetAttr(pPath & flDir) And _
            vbDirectory) = 16) Then sCldItm.Add pPath & flDir
            flDir = Dir 'next file
        Loop
    
    ' Subdirectories list(collection) processing
    For Each CldItm In sCldItm
        Call FlSrch(pFnd, CStr(CldItm), pMask, pSbDir) ' Recursive procedure call
    Next
    
    End Sub
    

    另外,请查看下面的链接。

    http://www.learnexcelmacro.com/wp/download/

    从名为“文件管理器(Excel 工作簿)”的链接中保存文件。这是一个非常酷的应用程序!

    【讨论】:

      猜你喜欢
      • 2018-06-08
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 2016-11-11
      • 2022-11-18
      • 1970-01-01
      • 2017-02-16
      相关资源
      最近更新 更多