I have the following vba code and when I try to run it, it says: Compile Error: Expected End Sub.
Anyone know what I'm doing wrong? I know NOTHING about vba. I want this to check for the last modified file and then tell me if it's more than 5 years old in a msgbox.
Sub LastModifiedFile()
Function FileLastModified(strFullFileName As String)
Dim fs As Object, f As Object, s As String
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(strFullFileName)
s = UCase(strFullFileName) & vbCrLf
s = s & "Last Modified: " & f.DateLastModified
FileLastModified = s
Set fs = Nothing: Set f = Nothing
If FileExists(strFullName) Then
MsgBox FileLastModified(strFullName)
Else
MsgBox "File Older than 5 Years : " & vbNewLine & strFullName
End If
End Function
End Sub
That code won't work as it is. You have the code from a function and code from a procedure intertwined with each other.
The first line defines your procedure:
You then have code relevant to the function.
This code should find out the last modified date of each file name passed to it and return that date to the main procedure... you can see the second from last line where it says
FileLastModified = s
. That's the line where it passes the value back to the calling procedure.Next you have some code from the main procedure again.
You can see on the second line where it's asking the
FileLastModified
function to look at the path of whichever file you pass to it.There's a problem here - the first line is calling for a function called
FileExists(strFullName)
. This is passing the file path to a function that you haven't posted asking if the file exists in the first place - it should return TRUE or FALSE.Another problem here is that if it can't find the file it says it's older than five years, rather than saying it doesn't exist at all.
After this bit of code there two lines - one ends the function and the other ends the sub.
Your code should look more like this:
You'll still need the code for the
FileExists
function though.