简体   繁体   English

Excel VBA检查并显示文件夹中的文件数

[英]Excel VBA check and display number of file in folder

I have the following module to check the number of files contained in a folder and display a messagebox with the with the number of files: 我有以下模块来检查文件夹中包含的文件数,并显示带有的消息框和文件数:

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

Files to look for (in separate module): 要查找的文件(在单独的模块中):

   Call CheckFiles("", "File1*.xlsx")
    Call CheckFiles("", "File2*.xlsx")

What I want to do is to only display messagebox if the number of files for File1 is not excaly 3 and the number of files for File2 is not excaly 2. This is what I'm having trouble doing? 我想做的是仅在File1的文件数量不是3且File2的文件数量不是2的情况下才显示消息框。这是我遇到的麻烦吗? How can this be acheived? 如何做到这一点?

Add the ChckNum as Third Parameter in the Subject and pass it in the Call Statement 在主题中将ChckNum添加为第三个参数并在Call语句中传递它

Try: 尝试:

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

And

  Call CheckFiles("", "File1*.xlsx", 3)
  Call CheckFiles("", "File2*.xlsx", 2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM