0

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? How can this be acheived?

1
  • 1
    Add the number as a third parameter of the sub. Also, If Right(strDir, 1) <> "\" Then strDir = strDir & "\" is unnecessary, since you already have an ending backslash from strDir = ThisWorkbook.Path & "\Source\". Commented Aug 20, 2019 at 13:35

1 Answer 1

1

Add the ChckNum as Third Parameter in the Subject and pass it in the Call Statement

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.