導航:首頁 > 文件處理 > vba子文件夾文件名字

vba子文件夾文件名字

發布時間:2023-01-22 23:51:09

『壹』 vba提取多層文件夾的*.xls文件,根據獲取到的文件名及路徑逐個打開*.xls文件,去判斷某幾列有多少個數據

『貳』 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

『叄』 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子文件夾文件名字相關的資料

熱點內容
唐宋八大家pdf 瀏覽:360
編譯原理中什麼叫推導 瀏覽:308
自己編譯系統好還是安裝好 瀏覽:292
編譯exescope 瀏覽:704
如何和好友一起玩伺服器 瀏覽:944
逗比程序員搞笑故事 瀏覽:860
送道是什麼app 瀏覽:455
亞信怎麼改伺服器地址 瀏覽:470
疫情期間怎麼疏解壓力 瀏覽:555
zip壓縮圖片 瀏覽:909
android應用運行時間 瀏覽:292
ug鑽孔沒有命令 瀏覽:954
編譯鏈接是程序開發步驟嗎 瀏覽:220
自學程序員要多久 瀏覽:727
雲伺服器上安裝兩個系統 瀏覽:402
編譯原理匹配計算理論 瀏覽:546
echo命令詳解 瀏覽:703
電子鎖加密是鎖加密還是鑰匙加密 瀏覽:428
虐殺原形1壓縮包 瀏覽:940
哪個app買車票便宜 瀏覽:169