Export excel to csv with a different separator

Цветан Пантев 0 Reputation points
2023-10-04T07:40:45.5866667+00:00

I want the macro to automatically save a specific workbook sheet in csv format. The macro works but uses " , " as separator. The program that reads the csv file requires the separator to be " ; ". I can't get the macro to replace the separator with the correct one.

Macro:

Sub ExportToCSV_v2()

Dim ws As Worksheet

Set ws = Worksheets("export")

Dim fileName As String

fileName = Left(ws.Parent.Name, 5)

Dim currentDir As String

currentDir = ThisWorkbook.Path

ws.SaveAs fileName:=currentDir & "" & fileName & ".csv", FileFormat:=xlCSV

End Sub

Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Excel | For business | Windows
{count} votes

1 answer

Sort by: Most helpful
  1. Tanay Prasad 2,250 Reputation points
    2023-10-13T05:45:13.6533333+00:00

    Hi,

    Try this code-

    Sub ExportToCSV_v2()     Dim ws As Worksheet     Set ws = Worksheets("export")      ' Set list separator to semicolon     Application.DecimalSeparator = ","     Application.ThousandsSeparator = " "     Application.UseSystemSeparators = False     Application.ListSeparator = ";"      ' Save the worksheet as CSV     Dim fileName As String     fileName = Left(ws.Parent.Name, 5)     Dim currentDir As String     currentDir = ThisWorkbook.Path     ws.SaveAs fileName:=currentDir & "\" & fileName & ".csv", FileFormat:=xlCSV      ' Reset list separator to default (comma)     Application.UseSystemSeparators = True End Sub 
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.