Freddie has attended:
Excel VBA Introduction course
Run time erro 1004
Hi Guys
I am having problems with the below code. I am getting a runtime erro 1004 and excel is telling me that the path is not recognised(?!) yet i've checked the path and it is correct - can you help?
Dim strFilePath As String
Dim MyFilea As String
Dim LatestFilea As String
Dim LatestDatea As Date
Dim LMDa As Date
strFilePath = "L:\ClientInterest\BAD-STATIC DATA\NA-rates\"
If Right(strFilePath, 1) <> "\" Then MyPatha = strFilePath & "\"
MyFilea = Dir(strFilePath & "*.xls", vbNormal)
Do While Len(MyFilea) > 0
LMDa = FileDateTime(strFilePath & MyFilea)
If LMDa > LatestDatea Then
LatestFilea = MyFile
LatestDatea = LMD
End If
MyFilea = Dir
Loop
Workbooks.Open strFilePath & LatestFilea
Best,
Freddie
RE: Run time erro 1004
Hi Freddie,
Thank you for the forum question.
I can spot an error in the below part of the code:
If LMDa > LatestDatea Then
LatestFilea = MyFile
LatestDatea = LMD
End If
Shouldn't it be:
If LMDa > LatestDatea Then
LatestFilea = MyFilea
LatestDatea = LMDa
End If
If you have OPTION EXPLICIT on top of your module Excel will point at this when you execute the code.
I hope that this will solve the issue.
If you Step your code with the locals window open you can follow the content of your variables and this can be very helpful with problems like this.
Kind regards
Jens Bonde
Microsoft Office Specialist Trainer
Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us
London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector