【问题标题】:Excel VBA, import the names of all files in the folder without using Application.FileDialog()Excel VBA,在不使用Application.FileDialog()的情况下导入文件夹中所有文件的名称
【发布时间】:2017-10-19 10:23:01
【问题描述】:

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

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

另一个问题我有(重要的)xDirectory$、xFname$ 和 InitialFoldr$ 变量末尾的 $ 符号是什么,为什么我不能声明它们 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 基础知识,但我仍然不明白很多东西。 请回答 $ 符号问题:-)

【问题讨论】:

  • Dim xFname$Dim xFname as String 的简写,只是可读性较差,所以通常你不会经常看到它。

标签: vba excel


【解决方案1】:

废弃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 的更多信息。

【讨论】:

    【解决方案2】:

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

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

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

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

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

    至于“$”部分,请参见上面@BrakNicku 的回答。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 2015-10-23
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多