VBA Function - Check all req files present in a Folder

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
983
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I want VBA Function which will check whether all required input files present in a folder.
if files missing highlight missing files.

Below are Main Key words to check
Debtors
Creditors
Overdue
Recon
Sales Register.

Below attmpted code, not giving correct result. I am ok with any other way.
VBA Code:
Sub Check_Files()

    Dim Filenames As String
    Dim path As String
    Filenames = "Debtors,Creditors,Overdue,Recon,Sales Register"
    path = "C:\Users\ASUS\Desktop\Reconcilation\"
    
   If Not Files_Check(path, Filenames) Then
        MsgBox "Files not found" & Chr(10) & Filenames, 16, "Files Missing in " & path
    
    End If

End Sub



Function Files_Check(ByVal path As String, ByRef Filenames As String) As Boolean
    Dim arFilename() As String
    arFilename = Split(Filenames, ",")
    Dim filesnames As String
    
    Dim i As Long
    
    Dim str As String
    str = ""
    
    Dim strFile As String
    
    For i = 0 To UBound(arFilename)
        strFile = "*" & arFilename(i) & "*.xlsx"
        Filenames = Dir(path & strFile)
        If Len(Filenames) = 0 Then
            Filenames = Filenames & "," & arFilename(i)
        End If
           
    Next i

Files_Check = CBool(Len(Filenames) = 0)
 

End Function
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try this:
VBA Code:
Public Sub Check_Files()

    Dim filenames As String
    Dim path As String
   
    filenames = "Debtors,Creditors,Overdue,Recon,Sales Register"
    path = "C:\Users\ASUS\Desktop\Reconcilation\"
       
    If Not Files_Check(path, filenames) Then
        MsgBox "Files not found in " & path & vbLf & filenames, vbCritical, "Files Missing"
    End If

End Sub


Private Function Files_Check(ByVal path As String, ByRef filenames As String) As Boolean

    Dim arrFilenames() As String
    Dim i As Long
    Dim file As String
   
    If Right(path, 1) <> "\" Then path = path & "\"
   
    arrFilenames = Split(filenames, ",")
    filenames = ""
   
    For i = 0 To UBound(arrFilenames)
        file = Dir(path & "*" & arrFilenames(i) & "*.xlsx")
        If file = vbNullString Then
            filenames = filenames & arrFilenames(i) & ","
        End If
    Next i

    If filenames <> "" Then filenames = Left(filenames, Len(filenames) - 1)
    Files_Check = CBool(Len(filenames) = 0)

End Function
 
Upvote 0
Hi John,

Thanks for your help, it worked as expected. (y)


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top