![](/img/trans.png)
[英]Excel VBA loop folder and check if Excel file name is equal to worksheet
[英]Excel VBA check and display number of file in folder
我有以下模塊來檢查文件夾中包含的文件數,並顯示帶有的消息框和文件數:
Sub CheckFiles(strDir As String, strType As String)
Dim file As Variant, i As Integer
strDir = ThisWorkbook.Path & "\Source\"
If Right(strDir, 1) <> "\" Then strDir = strDir & "\"
file = Dir(strDir & strType)
While (file <> "")
i = i + 1
file = Dir
Wend
MsgBox i
End Sub
要查找的文件(在單獨的模塊中):
Call CheckFiles("", "File1*.xlsx")
Call CheckFiles("", "File2*.xlsx")
我想做的是僅在File1的文件數量不是3且File2的文件數量不是2的情況下才顯示消息框。這是我遇到的麻煩嗎? 如何做到這一點?
在主題中將ChckNum
添加為第三個參數並在Call語句中傳遞它
嘗試:
Sub CheckFiles(strDir As String, strType As String, chknum As Integer)
Dim file As Variant, i As Integer
strDir = ThisWorkbook.path & "\Source\"
If Right(strDir, 1) <> "\" Then strDir = strDir & "\"
file = Dir(strDir & strType)
While (file <> "")
i = i + 1
file = Dir
Wend
If i <> chknum Then MsgBox i
End Sub
和
Call CheckFiles("", "File1*.xlsx", 3)
Call CheckFiles("", "File2*.xlsx", 2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.