+ Reply to Thread
Results 1 to 5 of 5

Thread: CSV export with Russian letters

  1. #1
    Registered User
    Join Date
    06-20-2009
    Location
    Belgrade, Serbia
    MS-Off Ver
    Excel 2007
    Posts
    35

    CSV export with Russian letters

    I started similar thread http://www.excelforum.com/excel-prog...ng-to-csv.html but i feel i didn't explain my problem exactly so it led to confusion with the guys who was willing to hep me. I ask moderators if its a problem for me starting another thread on the same topic please delete the first one.
    So...
    First of all i need to explain why I need this because its the usual question that people ask me. I use some excel file for Autocad automation. Im basicaly fillin the tables in Autocad from the data in Excel. I export the CSV in a separate file from that same excel (Im using a macro for that) and use that in Autocad. It all works fine when using English keyboard. Coleagues from Moscow asked me if i could do the same automation For CAD in their office. Of course they all use russian letters in their work. Problem occurs when i save the csv from the excel i use in Russian instead of letters i get a lot of question marks. I didnt even bother to import that in Autocad. But the thing that works is hex representation of the given letter.
    For example,
    ц \U+0446
    у \U+0443
    к \U+043A
    е \U+0435
    н \U+043D
    г \U+0433
    ш \U+0448
    щ \U+0449
    з \U+0437
    х \U+0445
    ъ \U+044A
    ф \U+0444
    ы \U+044B
    в \U+0432
    а \U+0430
    п \U+043F
    р \U+0440
    о \U+043E
    л \U+043B
    д \U+0434
    ж \U+0436
    э \U+044D
    я \U+044F
    ч \U+0447
    с \U+0441
    м \U+043C
    и \U+0438
    т \U+0442
    ь \U+044C
    б \U+0431
    ю \U+044E
    When imported in CAD this works just fine. Russian letters appear.
    So my question is, is there a way to translate the Russian letters in above stated manner, in the moment when macro saves the csv. To be perfectly clear, i want my excel unchanged, but the csv translated.
    This is the code i use for csv save as:
    Code:

    Sub write_csv_xls()     
    Application.DisplayAlerts = False          
    xls = ActiveWorkbook.FullName     
    Length = Len(xls) - 3     
    csv = Left(xls, Length) & "csv"         
     ActiveWorkbook.SaveAs Filename:= _        
     csv, FileFormat:=xlCSV, _         CreateBackup:=False      
    'Delete the Existing copy of the file so we can save it back over it    
     'without the write-protected error     
    Kill xls     ActiveWorkbook.SaveAs Filename:= _        
     xls, FileFormat:=xlNormal, _         
    Password:="", WriteResPassword:="", 
    ReadOnlyRecommended:= _        
     False, CreateBackup:=False              
    Application.DisplayAlerts = True 
    End Sub
    Last edited by sakinen; 06-27-2011 at 01:44 PM.

  2. #2
    Valued Forum Contributor tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    USA
    MS-Off Ver
    Excel 2003 - 2007
    Posts
    2,352

    Re: CSV export with Russian letters

    sakinen,

    Give the following code a try. It uses a second worksheet (named "Conversion") to get the values to find (column A) and replace with (column B):
    Sub write_csv_xls()
        
        Dim xlsPath As String: xlsPath = ActiveWorkbook.FullName
        Dim FileExt As String: FileExt = StrReverse(Left(StrReverse(xlsPath), InStr(StrReverse(xlsPath), ".")))
        ActiveWorkbook.SaveAs Filename:=Replace(xlsPath, FileExt, ".csv"), FileFormat:=xlCSV
        Dim wbCSV As Workbook: Set wbCSV = ActiveWorkbook
        
        Dim wsCnvt As Worksheet: Set wsCnvt = Sheets("Conversion")
        Dim strFind() As Variant, strReplace() As Variant, arrIndex As Long
        strFind = wsCnvt.Range("A1", wsCnvt.Cells(Rows.Count, "A").End(xlUp)).Value
        strReplace = wsCnvt.Range("B1", wsCnvt.Cells(Rows.Count, "B").End(xlUp)).Value
        
        Dim allCells() As Variant: allCells = wbCSV.ActiveSheet.UsedRange.Value
        Dim r As Long, c As Long
        For r = 1 To UBound(allCells, 1)
            For c = 1 To UBound(allCells, 2)
                For arrIndex = 1 To UBound(strFind)
                    allCells(r, c) = Replace(allCells(r, c), strFind(arrIndex, 1), strReplace(arrIndex, 1))
                Next arrIndex
            Next c
        Next r
        ActiveSheet.UsedRange.Value = allCells
        
        Workbooks.Open xlsPath
        wbCSV.Close True
        
    End Sub


    Hope that helps,
    ~tigeravatar
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    06-20-2009
    Location
    Belgrade, Serbia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: CSV export with Russian letters

    Wow. Works like a charm. Bow to the master!
    I have a couple of questions.
    If i understood the code i can write down more letters to translate i want in a conversion table.
    Is there a way to insert a few rows of code that kills previous csv without asking to overwrite it. I tried to use from a previous code but something got wrong.
    Thanks a lot again!

  4. #4
    Valued Forum Contributor tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    USA
    MS-Off Ver
    Excel 2003 - 2007
    Posts
    2,352

    Re: CSV export with Russian letters

    sakinen,

    To prevent it from asking if you want to override it, just wrap it in Application.DisplayAlerts = False/True as follows:
    Sub write_csv_xls()
        
        Application.DisplayAlerts = False
        
        Dim xlsPath As String: xlsPath = ActiveWorkbook.FullName
        Dim FileExt As String: FileExt = StrReverse(Left(StrReverse(xlsPath), InStr(StrReverse(xlsPath), ".")))
        ActiveWorkbook.SaveAs Filename:=Replace(xlsPath, FileExt, ".csv"), FileFormat:=xlCSV
        Dim wbCSV As Workbook: Set wbCSV = ActiveWorkbook
        
        Dim wsCnvt As Worksheet: Set wsCnvt = Sheets("Conversion")
        Dim strFind() As Variant, strReplace() As Variant, arrIndex As Long
        strFind = wsCnvt.Range("A1", wsCnvt.Cells(Rows.Count, "A").End(xlUp)).Value
        strReplace = wsCnvt.Range("B1", wsCnvt.Cells(Rows.Count, "B").End(xlUp)).Value
        
        Dim allCells() As Variant: allCells = wbCSV.ActiveSheet.UsedRange.Value
        Dim r As Long, c As Long
        For r = 1 To UBound(allCells, 1)
            For c = 1 To UBound(allCells, 2)
                For arrIndex = 1 To UBound(strFind)
                    allCells(r, c) = Replace(allCells(r, c), strFind(arrIndex, 1), strReplace(arrIndex, 1))
                Next arrIndex
            Next c
        Next r
        ActiveSheet.UsedRange.Value = allCells
        
        Workbooks.Open xlsPath
        wbCSV.Close True
        
        Application.DisplayAlerts = True
        
    End Sub


    Hope that helps,
    ~tigeravatar

  5. #5
    Registered User
    Join Date
    06-20-2009
    Location
    Belgrade, Serbia
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: CSV export with Russian letters

    Thanks a lot man. It works fine.
    The only thing left that i noticed just a minute ago is that if i change something and do not save excel, after running a macro changed values return to the previous ones. Nothing to worry abut thou.
    Thanks again!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0