Using VBA Dir with special characters.

alfranco17

Board Regular
Joined
Apr 14, 2013
Messages
198
Hi.

I am trying to use the DIR function to loop through all the files in a folder. This is my code:

Code:
Sub OpenAll()    vPath = "C:\Users\Armando\Desktop\"
    ChDir vPath
    
    NextFile = Dir(vPath & "\*.xlsx")
    Do While NextFile <> ""
        Workbooks.Open Filename:=NextFile
        NextFile = Dir
    Loop
End Sub

However, when the name includes a character such as í, I get an error message like this one:

Run-time error '1004':

Sorry, we could not find Cfvico.xlsx. Is it possible it was moved, renamed or deleted?

Windows shows the file name is "Cφvico".

The name should be "Cívico".

Is there a way to get around accented characters in filenames?

Thanks.
Armando
 
Try:

Code:
[COLOR=#333333]Sub OpenAll()
    vPath = "C:\Users\Armando\Desktop\"
[/COLOR]    ChDir vPath
    
    NextFile = Dir(vPath & "\*.xlsx")
    Do While NextFile <> ""
        [COLOR=#0000ff]Workbooks.Open Filename:=vPath & NextFile[/COLOR]
        NextFile = Dir
    Loop [COLOR=#333333]End Sub[/COLOR]
 
Upvote 0
Dir doesn't work with file names with Unicode characters; it returns the ANSI equivalent to the filename, which you cannot use to open the file. VB includes a DirW function that doesn’t exist in VBA.

You need to use the FileSystemObject. Or change the file name.
 
Upvote 0
Dir doesn't work with file names with Unicode characters; it returns the ANSI equivalent to the filename, which you cannot use to open the file. VB includes a DirW function that doesn’t exist in VBA.

You need to use the FileSystemObject. Or change the file name.
But the code I posted above worked for me. It opened many files whose name is in Chinese.
 
Upvote 0
But the code I posted above worked for me. It opened many files whose name is in Chinese.
OK. Maybe that is because in Control Panel/Region/Administrative/Change System Locale, I set it to Chinese. So, Dir now works with Chinese.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,226,835
Messages
6,193,232
Members
453,781
Latest member
Buzby

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