導航:首頁 > 文件教程 > vba獲取文件夾下所有文件名

vba獲取文件夾下所有文件名

發布時間:2023-05-27 09:22:33

① 用EXCEL VBA獲取指定目錄下的文件名(包括文件夾名)

1、首先利用快捷鍵「Windows鍵+R」打開「運行」窗口

② excel怎樣用vba自動提取文件夾內的文件名

excel中用vba實現自動提取文件夾內的文件名的方法如下:

1、新建一個vba宏腳本

③ VBA獲取某文件夾下所有文件和子文件目錄的文件

【引用位置】 https://blog.csdn.net/pashine/article/details/42100237

'-------------------------------------------
'獲取某文件夾下的所有Excel文件
'-------------------------------------------
Sub getExcelFile(sFolderPath As String)
On Error Resume Next
Dim f As String
Dim file() As String
Dim x
k = 1

ReDim file(1)
file(1) = sFolderPath & ""

End Sub

'-------------------------------------------
'獲取某文件夾下的所有文件和子目錄下的文件
'-------------------------------------------
Sub getAllFile(sFolderPath As String)
'Columns(1).Delete
On Error Resume Next
Dim f As String
Dim file() As String
Dim i, k, x
x = 1
i = 1
k = 1

ReDim file(1 To i)
file(1) = sFolderPath & ""

'-- 獲得所有子目錄
Do Until i > k
f = Dir(file(i), vbDirectory)
Do Until f = ""
If InStr(f, ".") = 0 Then
k = k + 1
ReDim Preserve file(1 To k)
file(k) = file(i) & f & ""
End If
f = Dir
Loop
i = i + 1
Loop

'-- 獲得所有子目錄下的所有文件
For i = 1 To k
f = Dir(file(i) & " . ") '通配符 . 表示所有文件,*.xlsx Excel文件
Do Until f = ""
'Range("a" & x) = f
Range("a" & x).Hyperlinks.Add Anchor:=Range("a" & x), Address:=file(i) & f, TextToDisplay:=f
x = x + 1
f = Dir
Loop
Next
End Sub

④ vba獲取路徑下所有文件名和對應的文件路徑,並且顯示到下面表格中。怎麼做

⑤ Excel中VBA提取文件夾名稱的方法

文章介紹excel中使用vba提取文件名的操作步驟。根據需要自行修改vba提取文件名的路徑和存放單元格即可。
在excel中使用VBA編寫代碼,可以輕松的提取某個文件夾下面的所有文件名。
比如筆者在F盤下面建立了一個文件夾,文件夾的名稱是:office教程網,現在想將「office教程網」這個文件夾下面的所有文件名提取出來放在當前工作表的C列。
具體的vba提取文件名的操作如下:
1.按下ALT+F11,打開VBE編輯器。
2.執行「插入——模塊」,插入模塊1。

3.在右邊的代碼編輯窗口,復制下面的代碼,然後單擊「綠色箭頭」或者快捷鍵F5鍵,運行代碼。
Private Sub vba提取文件名()
Dim FileName As String
Dim i As Long
FileName = Dir("F:\office教程網\*.*")
i = 0
Range("C:C").ClearContents
Do While FileName > ""
i = i + 1
Cells(i, 3) = FileName
FileName = Dir
Loop
End Sub

4.關閉VBE窗口,回到工作表中,可以在C列看到F盤「office教程網」文件夾下面所有的文件名全部羅列在C列了。

關於上面的vba提取文件名的代碼,請在實際使用時,根據需要修改提取文件名的路徑(F:\office教程網\*.*),以及存放在C列的位置(Cells(i, 3))。

⑥ 用EXCEL VBA獲取指定目錄下的文件名(包括文件夾名)

1、首先利用快捷鍵「Windows鍵+R」打開「運行」窗口

⑦ VBA 如何取C盤下和C盤文件夾下所有Excel文件名稱,並將這些文件名放到Excel文件上。

VBA遍歷文件夾常用有三種方法,這三種方法中,filesearch不適合2007和2010版本,而且速度比較慢,遞歸法速度也慢。只有用DIR加循環的方法,速度飛快。下面是三種方法的代碼:
1、filesearch法
Sub test3()
Dim wb As Workbook
Dim i As Long
Dim t
t = Timer
With Application.FileSearch '調用fileserch對象
.NewSearch '開始新的搜索
.LookIn = ThisWorkbook.path '設置搜索的路徑
.SearchSubFolders = True '搜索范圍包括 LookIn 屬性指定的文件夾中的所有子文件夾
.Filename = "*.xls" '設置搜索的文件類型
' .FileType = msoFileTypeExcelWorkbooks
If .Execute() > 0 Then '如果找到文件
For i = 1 To .FoundFiles.Count
'On Error Resume Next
Cells(i, 1) = .FoundFiles(i) '把找到的文件放在單元格里
Next i
Else
MsgBox "沒找到文件"
End If
End With
MsgBox Timer - t
End Sub
2、遞歸法
Sub Test()
Dim iPath As String, i As Long
Dim t
t = Timer
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "請選擇要查找的文件夾"
If .Show Then
iPath = .SelectedItems(1)
End If
End With

If iPath = "False" Or Len(iPath) = 0 Then Exit Sub

i = 1
Call GetFolderFile(iPath, i)
MsgBox Timer - t
MsgBox "文件名鏈接獲取完畢。", vbOKOnly, "提示"

End Sub
Private Sub GetFolderFile(ByVal nPath As String, ByRef iCount As Long)
Dim iFileSys
'Dim iFile As Files, gFile As File
'Dim iFolder As Folder, sFolder As Folders, nFolder As Folder
Set iFileSys = CreateObject("Scripting.FileSystemObject")
Set iFolder = iFileSys.GetFolder(nPath)
Set sFolder = iFolder.SubFolders
Set iFile = iFolder.Files
With ActiveSheet
For Each gFile In iFile
' .Hyperlinks.Add anchor:=.Cells(iCount, 1), Address:=gFile.path, TextToDisplay:=gFile.Name
iCount = iCount + 1
Next
End With

'遞歸遍歷所有子文件夾
For Each nFolder In sFolder
Call GetFolderFile(nFolder.path, iCount)
Next
End Sub
3、dir循環法
Sub Test() '使用雙字典,旨在提高速度
Dim MyName, Dic, Did, i, t, F, TT, MyFileName
'On Error Resume Next
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder(0, "選擇文件夾", 0, 0)
If Not objFolder Is Nothing Then lj = objFolder.self.path & "\"
Set objFolder = Nothing
Set objShell = Nothing
t = Time
Set Dic = CreateObject("Scripting.Dictionary") '創建一個字典對象
Set Did = CreateObject("Scripting.Dictionary")
Dic.Add (lj), ""
i = 0
Do While i < Dic.Count
Ke = Dic.keys '開始遍歷字典
MyName = Dir(Ke(i), vbDirectory) '查找目錄
Do While MyName <> ""
If MyName <> "." And MyName <> ".." Then
If (GetAttr(Ke(i) & MyName) And vbDirectory) = vbDirectory Then '如果是次級目錄
Dic.Add (Ke(i) & MyName & "\"), "" '就往字典中添加這個次級目錄名作為一個條目
End If
End If
MyName = Dir '繼續遍歷尋找
Loop
i = i + 1
Loop
Did.Add ("文件清單"), "" '以查找D盤下所有EXCEL文件為例
For Each Ke In Dic.keys
MyFileName = Dir(Ke & "*.xls")
Do While MyFileName <> ""
Did.Add (Ke & MyFileName), ""
MyFileName = Dir
Loop
Next
For Each Sh In ThisWorkbook.Worksheets
If Sh.Name = "XLS文件清單" Then
Sheets("XLS文件清單").Cells.Delete
F = True
Exit For
Else
F = False
End If
Next
If Not F Then
Sheets.Add.Name = "XLS文件清單"
End If
Sheets("XLS文件清單").[A1].Resize(Did.Count, 1) = WorksheetFunction.Transpose(Did.keys)
TT = Time - t
MsgBox Minute(TT) & "分" & Second(TT) & "秒"
End Sub

⑧ Excel VBA列出某文件夾下子文件夾及文件名

遍歷文件夾 並列出文件 & 文件夾 名 代碼如下:

在文件夾內 新建 個 Excel文件

Excel文件內 按 Alt+F11 視圖--代碼窗口, 把如下代碼復制進去, F5運行

Sub遍歷文件夾()
'OnErrorResumeNext
Dimfn(1To10000)AsString
Dimf,i,k,f2,f3,x
Dimarr1(1To100000,1To1)AsString,qAsInteger
Dimt
t=Timer
fn(1)=ThisWorkbook.path&""
i=1:k=1
DoWhilei<UBound(fn)
Iffn(i)=""ThenExitDo
f=Dir(fn(i),vbDirectory)
Do
IfInStr(f,".")=0Andf<>""Then
k=k+1
fn(k)=fn(i)&f&""
EndIf
f=Dir
LoopUntilf=""
i=i+1
Loop
'*******下面是提取各個文件夾的文件***
Forx=1ToUBound(fn)
Iffn(x)=""ThenExitFor
f3=Dir(fn(x)&"*.*")
DoWhilef3<>""
q=q+1
arr1(q,1)=fn(x)&f3
f3=Dir
Loop
Nextx
ActiveSheet.UsedRange=""
Range("a1").Resize(q)=arr1
MsgBoxFormat(Timer-t,"0.00000")
EndSub

效果如圖:



⑨ 用VBA代碼如何獲得指定文件夾內的所有子文件夾名稱

Set fso=CreateObject("Scripting.FileSystemObject")
Set folder=fso.GetFolder("D:")
dim a() as string
dim b
b=1
for each thing in folder.subfolders
addfolder fso,thing,a,b
next
msgbox b

function addfolder(byref fso,byref dir,byref a,byref b)
set folder=fso.getfolder(dir)
a(b)=dir
b=b+1
For Each thing in folder.SubFolders
set folder=fso.getfolder(thing)
a(b)=thing
b=b+1
addfolder fso,thing,a,b
Next
end function

⑩ vba 遍歷指定文件夾(含子目錄)獲取文件名,哪種方法速度最快

Sub LoopAllExcelFilesInFolder()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog

'這里很關鍵,決定宏執行快慢的關鍵
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

'打開目錄選擇框
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)

With FldrPicker
.Title = "請選擇目錄"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With

'取消選擇
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings

'指定過濾的文件後綴
myExtension = "*.xls*"

'遍歷全路徑
myFile = Dir(myPath & myExtension)

'循環處理每一個文件
Do While myFile <> ""
'打開
Set wb = Workbooks.Open(Filename:=myPath & myFile)

'確保工作簿被打開,在處理下一個文件時
DoEvents

'設置背景色
wb.Worksheets(1).Range("A1:Z1").Interior.Color = RGB(51, 98, 174)

'保存工作簿
wb.Close SaveChanges:=True

'確保工作簿被關閉,在處理下一個文件時
DoEvents

'接著處理下一個
myFile = Dir
Loop

'提示處理完成
MsgBox "處理完成!"

ResetSettings:
'恢復設置
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

閱讀全文

與vba獲取文件夾下所有文件名相關的資料

熱點內容
如何打開ps保存的文件 瀏覽:744
幼兒編程教育有哪些 瀏覽:453
汽車發外地用什麼app 瀏覽:810
網路如何贊美女人漂亮 瀏覽:143
如何把桌面文件放到excel裡面 瀏覽:363
照片文件名是怎麼查的 瀏覽:876
c怎麼在cmd模式下顯示文件 瀏覽:325
手機怎麼把文件夾的圖片移到相冊 瀏覽:440
hjc是啥文件的格式 瀏覽:298
報廢鐵皮文件櫃圖片 瀏覽:801
win10系統更新文件能 瀏覽:558
怎麼讓蘋果手機下載其他APP 瀏覽:471
多個cs文件編譯成一個dll 瀏覽:606
sql管理工具70 瀏覽:130
js裡面的圖片對齊 瀏覽:965
三星2016視頻文件夾 瀏覽:317
舊手機創新手機數據怎麼傳 瀏覽:954
怎麼刪除領克app里的記錄 瀏覽:254
捷波朗弦月3最新版本 瀏覽:123
win10保存不了文件 瀏覽:735

友情鏈接