導航:首頁 > 文件處理 > 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子文件夾文件名字相關的資料

熱點內容
單片機入門視頻教程第二課 瀏覽:45
天然氣可壓縮多少倍 瀏覽:238
程序員面試評價 瀏覽:128
手錶加密門禁卡 瀏覽:238
psf已加密 瀏覽:553
返利app怎麼用教程 瀏覽:888
一個程序員寫的詩火了 瀏覽:613
red攝影機壓縮比 瀏覽:410
app賬號怎麼弄 瀏覽:823
hc0551單片機 瀏覽:755
噴淋管道支架演算法 瀏覽:592
壓縮機壞了電腦無法啟動 瀏覽:241
不小心加密了怎麼去掉 瀏覽:955
linux命令行連接db2 瀏覽:898
洪恩app怎麼設置護眼模式 瀏覽:971
小猿口算app怎麼選課時 瀏覽:421
手機號脫敏加密解密 瀏覽:169
台電加密u盤多少錢 瀏覽:547
控制面板文件夾怎麼隱藏 瀏覽:846
opticspdf 瀏覽:594