|
用EXCEL VBA获取指定目录下的文件名(包括文件夹名)在网上看到的,觉得很好,所以拿来分享了
出处http://zhidao.baidu.com/link?url ... vT0n7oMLcbP-9s4gQm_- Sub GetFoldersAndFiles()
- Dim arr()
- With Application.FileDialog(msoFileDialogFolderPicker)
- .InitialFileName = "C:"
- If .Show = True Then Path = .SelectedItems(1) & ""
- End With
- If Path = "" Then Exit Sub
- MyName = Dir(Path, vbDirectory) '查找目录
- Do
- If MyName <> "." And MyName <> ".." Then
- n = n + 1
- ReDim Preserve arr(1 To n)
- arr(n) = IIf((GetAttr(Path & MyName) And vbDirectory) = vbDirectory, "<" & MyName & ">", MyName)
- End If
- MyName = Dir
- Loop While MyName <> ""
- Sheet1.Range("A1").Resize(n, 1) = WorksheetFunction.Transpose(arr)
- Sheet1.Range("A:A").Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
- OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _
- :=xlPinYin, DataOption1:=xlSortNormal
-
- End Sub
复制代码 |
|