dir[(pathname[,attributes])]
構建測試環境如下:
一、測試在dir函式中使用萬用字元來查詢多個檔案,在vbe中輸入**如下:
sub listfiles()
dim strpath as string, strtmp as string
strpath = "c:\test\"
strtmp = dir(strpath & "*.txt")
do while strtmp <> ""
debug.print strpath & strtmp
strtmp = dir
loop
end sub
本意為查詢所有txt文件,得到結果如下:
c:\test\vba.txt
c:\test\新建文字文件.txtd
c:\test\新建文字文件 (2).txt
程式執行結果中不但包括txt文件,還包含txtd檔案。
二、測試attributes引數,**如下:
sub listsubfolder()
dim strpath as string, strtmp as string
strpath = "c:\test\"
strtmp = dir(strpath & "vb*", vbdirectory)
do while strtmp <> ""
debug.print strpath & strtmp
strtmp = dir
loop
end sub
本意為查詢vb開頭的資料夾,程式執行結果為:
c:\test\vba
c:\test\vba.txt
結果中不但包含了vba資料夾,還包含vba.txt檔案。
三、感想及小結
1.dir函式是完全按位元組匹配pathname引數,而不是按型別。
2.attributes引數是包含關係,而不是獨有關係。
3.如只想獲得資料夾,需使用條件判斷語句。例,if getattr(strpath) and vbdirectory then .
VBA中Dir函式的使用
dir 會返回匹配 pathname 的第乙個檔名。若想得到其它匹配 pathname 的檔名,再一次呼叫dir,且不要使用引數。如果已沒有合乎條件的檔案,則 dir 會返回乙個零長度字串 一旦返回值為零長度字串,並要再次呼叫 dir 時,就必須指定 pathname,否則會產生錯誤。檔案數確定時,...
vba 自動換行 VBA中msgbox的用法小結
在訊息框中顯示資訊,並等待使用者單擊按鈕,可返回單擊的按鈕值 比如 確定 或者 取消 通常用作顯示變數值的一種方式。msgbox prompt buttons title helpfile,context 引數說明 1 prompt,必需的引數,為字串,作為顯示在訊息框中的訊息文字。其最大長度約為1...
VBA中的函式Timer用法
第1.40例 timer 函式 一 題目 要求編寫一段 運用 timer 函式來計算本 執行所化的時間。二 sub 示例 1 040 dim t,i a t timer for i 1 to 1000000 a a i next i msgbox timer t 秒 返回0.046875秒 end ...