Excel VBA,导入文件夹中所有文件的名称,而不使用Application.FileDialog()

时间:2017-05-19 10:20:25

标签: excel vba excel-vba

我有一个文件夹(每次都是同一个文件夹 - 所以我不需要浪费时间用Application.FileDialog来选择它)我需要将所有文件名提取到Excel列中下进行。

这是我在堆栈上使用Application.FileDialog()找到的代码,但是我想硬编码文件夹的路径(C:\ Users \ michal \ SkyDrive \ csv \ bossa \ mstcgl_csv)。

另一个问题我(重要)xDirectory $,xFname $和InitialFoldr $变量末尾的$ sign是什么,为什么我不能声明它们as String? ? 这些变量不是字符串吗? ?

这是代码:

Sub GetFileNames()    
  Dim Lista As Worksheet              
  Dim xRow As Long                     
  Dim xDirectory$                     
  Dim xFname$                         
  Dim InitialFoldr$

  Dim start As Double    
  Dim finish As Double    
  Dim total_time As Double

  start = Timer              ' remember time when macro starts.

     ThisWorkbook.Sheets("Lista").Range("C1").Select

     InitialFoldr$ = "C:\Users\michal\SkyDrive\csv\bossa\"

     With Application.FileDialog(msoFileDialogFolderPicker)
         .InitialFileName = Application.DefaultFilePath & "\"
         .Title = "Please select a folder to list Files from"
         .InitialFileName = InitialFoldr$
         .Show                           ' creates list of files  ? ? ?

          If .SelectedItems.Count <> 0 Then                  
                xDirectory$ = .SelectedItems(1) & "\"  
                xFname$ = Dir(xDirectory$, 7)
                    Do While xFname$ <> ""
                         ThisWorkbook.Sheets("Lista").ActiveCell.Offset(xRow, 0) = xFname$
                         ActiveCell.Offset(xRow) = xFname$                         
                         xRow = xRow + 1             
                         xFname$ = Dir               
                    Loop                            
         End If
    End With
   finish = Timer                   ' Set end time.   
  total_time = Round(finish - start, 3)    ' Calculate total time.   
  MsgBox "This code ran successfully in " & total_time & " seconds", vbInformation
End Sub

你们可以帮帮我吗? 我只是学习了我的VBA基础知识,但我还是不了解很多东西。 请回答$ sign问题: - )

2 个答案:

答案 0 :(得分:2)

此行将Application.FileDialog的路径写入字符串:

xDirectory$ = .SelectedItems(1) & "\" 

现在您只需要将文件夹硬编码为该字符串:

xDirectory$ = "C:\Users\michal\SkyDrive\csv\bossa\"

并删除与FileDialog部分相关的所有代码。

至于&#34; $&#34;部分,请参阅@ BrakNicku上面的答案。

答案 1 :(得分:2)

废弃FileDialog并直接使用Dir功能:

Sub GetFileNames()

    Const InitialFoler As String = "C:\Users\michal\SkyDrive\csv\bossa\"

    Dim Lista As Worksheet
    Dim filename As String
    Dim xRow As Long
    Dim start As Double, finish As Double, total_time As Double

    start = Timer              ' remember time when macro starts.
    xRow = 1

    filename = Dir(InitialFoler & "*.*")

    With ThisWorkbook.Sheets("Lista")
        Do While Len(filename) > 0
            .Range("C" & xRow).Value = filename
            xRow = xRow + 1
            filename = Dir
        Loop
    End With

    finish = Timer                   ' Set end time.
    total_time = Round(finish - start, 3)    ' Calculate total time.

    MsgBox "This code ran successfully in " & total_time & " seconds", vbInformation
End Sub

您可以详细了解Dir函数here