I've got the following module checks for the number of files and writes to a .txt file when files are missing:
Sub CheckFiles(strDir As String, strType As String, chknum As Integer)
Dim file As Variant, i As Integer
Dim sFilename As String
strDir = ThisWorkbook.Path & "\Source\"
sFilename = ThisWorkbook.Path & "\Logs.txt"
If Right(strDir, 1) <> "\" Then strDir = strDir & "\"
file = Dir(strDir & strType)
While (file <> "")
i = i + 1
file = Dir
Wend
If i <> chknum Then
' Archive file at certain size
If FileLen(sFilename) > 20000 Then
FileCopy sFilename _
, Replace(sFilename, ".txt", Format(Now, "ddmmyyyy hhmmss.txt"))
Kill sFilename
End If
' Open the file to write
Dim filenumber As Variant
filenumber = FreeFile
Open sFilename For Append As #filenumber
Print #filenumber, CStr(Now) & ", " & "Source file: " & strType & " is missing " & chknm - i & " file(s)"
Close #filenumber
End If
End Sub
The CheckFiles sub is called from this module:
Sub Report()
'other code
Call CheckFiles("", "File1.xlsx", 1)
Call CheckFiles("", "File2.xlsx", 1)
Call CheckFiles("", "File3.xlsx", 1)
Call CheckFiles("", "File4*.xlsx", 24)
'other code
End Sub
What I'm trying to do is to exit the Report
sub if files are missing.
I've tried adding On Error Goto EH
at the beginning of CheckFiles
and adding the following at the end of the Report
sub when error is detected but it doesn't work.
Done:
Exit Sub
EH:
Call EmailError
End Sub
How can I achieve this?