如何列出文件夹

时间:2015-07-14 23:41:56

标签: excel vba

我正在尝试在文件夹中创建文件名列表以供参考,以下代码列出了扩展名为Filename.pdf的所有文件名

如何从文件名中排除扩展名? .pdf

Option Explicit
Sub GetFileName()
    Dim xlRow As Long
    Dim sDir As String
    Dim FileName As String
    Dim sFolder As String

    sFolder = "C:\Temp\"

    With Application.FileDialog(msoFileDialogFolderPicker)
        .InitialFileName = Application.DefaultFilePath & "\"
        .Title = "Please select a folder"
        .InitialFileName = sFolder
        .Show
        If .SelectedItems.Count <> 0 Then
            sDir = .SelectedItems(1) & "\"
            FileName = Dir(sDir, 7)

            Do While FileName <> ""
                Range("A1").Offset(xlRow) = FileName
                xlRow = xlRow + 1
                FileName = Dir
            Loop
        End If
    End With
End Sub

1 个答案:

答案 0 :(得分:2)

我不是100%肯定你在问什么,但我认为

If FileName Like "*.pdf" Then
    Range("A1").Offset(xlRow) = Mid(FileName,1,Len(FileName)-4)
End If

可能就是你追求的目标。