Home » excel » Filename Corrupted in VBA excel

Filename Corrupted in VBA excel

Posted by: admin April 23, 2020 Leave a comment


Out of the clear, blue sky, I am suddenly getting an error that I have never seen before, and I don’t know why it’s happening, because I have done nothing to cause the problem.

I am attempting to open a .csv file, which I have done successfully for many thousands of times. Here’s the code:

ChDir "KathyStringHD:Library:WebServer:Documents:DispatchReports:DispatchReportsFolder:"

NextFile = Dir("")

Do While NextFile <> ""

If NextFile <> ".DS_Store" Then
    Extension = Right(NextFile, 4)

    If Extension = ".csv" Then
         SaveFile = Left(NextFile, Len(NextFile) - 4) & ".xlsx"

MsgBox NextFile

         Call ProcessMonthlyReport(NextFile, Extension, SaveFile, sPath, SavePath)
     End If
End If

NextFile = Dir


Everything works except for one file. The name of the file is ‘ACTStillwater February, 2013.csv’, but when the routine tries to open this one file, the name is corrupted to read ‘ACTStillwater Februa#, 6 random hex numbers, then .csv’. The appendage looks like a color code, but I don’t have that one in the file itself, or in the PHP script that generated the file. When I go into Finder and get the file info, the name is fine there. As I said, what has me bum-fuzzled is that the error is only on one file.

How to&Answers:

It appears that there is a comma in the filename:

ACTStillwater February , 2013.csv

Try removing it:

ACTStillwater February 2013.csv

and see if it will run now.

See MSDN – Filename (Windows) for details


I suspect it is not the comma, but the fact that the filename has more than 32 characters.

I found that when opening a file in vba that had any one file or folder name in the entire path with more than 32 characters would fail.

This perhaps explains why the problem suddenly occurred:

‘ACT Stillwater February, 2013.csv’ has 33


‘ACT Stillwater January, 2013.csv

has 32

Just a thought.

If your path was

“KathyStringHD:Library:WebServer:Documents:DispatchReports:Dispatch Reports for the year Folder:”

It would also fail

Bob J.