Search code examples
excelvbautf-8character-encodingnon-english

Excel VBA non-english character support for reading files


I am using this piece of code for seperating sheets to single files:

Sub Splitbook()
Dim xPath As String
xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ThisWorkbook.Sheets
   xWs.Copy
   Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & xWs.Name & ".xlsx"
   Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

But I get these two errors respectively when I run the code: enter image description here enter image description here

and that file ".\VB5052.tmp" is being created at run time. I figured out that this error occurs when my path have non-english characters. So, how to configure VBA in order to support non-english characters?

By the way, I have many different paths and I am doing this seperation most times.

This Path didn't work: "D:\Yeni Klasör" However, when I remove the "ö" character: "D:\Yeni" this works. My sheet names include non-english characters but it is not the error. Just editing the path works.


Solution

  • The solution is just controlling the system locale on windows like that:

    1. Go to Control Panel,
    2. Click Clock, Language and Region
    3. Windows 10, Windows 8: Click Region
      Windows 7: Click Region and Language
      Windows XP: Click Regional and Language Options
      The Region and Language options dialog appears.
    4. Click the Administrative tab
      On Windows XP, click the Advanced tab
      If there is no Advanced tab, then you are not logged in with administrative privileges.
    5. Under the Language for non-Unicode programs section, click Change system locale and select the desired language.
    6. Click OK
    7. Restart the computer to apply the change.

    In the 5th step, choose the language that you need (i.e. the language that has your character). Otherwise your path will contain some different characters which cause errors at run time.