Convert CSV to XLSX keeping columns and unicode

chilly_bang

Board Regular
Joined
Jun 17, 2016
Messages
57
I have a directory with semicolon-delimited CSV files containing unicode signs. I try to convert them to XLSX using the solution from this thread, but fail in important parts:
the code makes XLSX from CSV, but all CSV data comes into the same column and all unicode signs are broken. However, if i use manually the legacy import wizard, with delimiter and collation selection, importing of CSV succeed always.

What should be adjusted in this code to keep columns and unicode healthy? An example of the data i work with is here: https://ufile.io/02e5bz6h

Code:
[COLOR=#333333]Sub ConvertCSVToXlsx()[/COLOR]    
    Dim myfile As String
    Dim oldfname As String, newfname As String
    Dim workfile
    Dim folderName As String
    
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
'   Capture name of current file
    myfile = ActiveWorkbook.Name
    
'   Set folder name to work through
    folderName = "C:\Test\"
    
'   Loop through all CSV filres in folder
    workfile = Dir(folderName & "*.CSV")
    Do While workfile <> ""
'       Open CSV file
        Workbooks.Open Filename:=folderName & workfile
'       Capture name of old CSV file
        oldfname = ActiveWorkbook.FullName
'       Convert to XLSX
        newfname = folderName & Left(ActiveWorkbook.Name, Len(ActiveWorkbook.Name) - 4) & ".xlsx"
        ActiveWorkbook.SaveAs Filename:=newfname, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        ActiveWorkbook.Close
'       Delete old CSV file
        Kill oldfname
        Windows(myfile).Activate
        workfile = Dir()
    Loop
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
 [COLOR=#333333]End Sub[/COLOR]
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try this macro:
Code:
Public Sub Convert_CSV_To_XLSX()
    
    Dim folderPath As String, csvFileName As String
    Dim csvWorkbook As Workbook
    Dim qt As QueryTable
    
    folderPath = "C:\Test\"
    If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
            
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
            
    Set csvWorkbook = Workbooks.Add(xlWBATWorksheet)

    csvFileName = Dir(folderPath & "*.csv")
    Do While csvFileName <> vbNullString
    
        csvWorkbook.Worksheets(1).Cells.ClearContents
        Set qt = csvWorkbook.Worksheets(1).QueryTables.Add(Connection:="TEXT;" & folderPath & csvFileName, Destination:=csvWorkbook.Worksheets(1).Range("A1"))
        With qt
            .TextFileParseType = xlDelimited
            .TextFileSemicolonDelimiter = True
            .TextFilePlatform = 65001
            .Refresh BackgroundQuery:=False
        End With
        qt.Delete
        
        'Save as .xlsx file and delete original .csv file
        
        csvWorkbook.SaveAs FileName:=folderPath & Left(csvFileName, InStrRev(csvFileName, ".") - 1) & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
        Kill folderPath & csvFileName
        
        csvFileName = Dir()
    Loop
    
    csvWorkbook.Close False
    
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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