Macro to save a CSV with a tilde as delimiter

David Montoya

New Member
Joined
Apr 25, 2018
Messages
49
Gentlemen,

Using a VBA macro, is there a way to save a CSV file a tilde delimiter instead of comma?

Thank you
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
You can change the configuration temporarily to save these files.

Enter control panel

win81.png


Press Additional settings.

region-control-panel.jpg




Change List separator by tilde


customize_format_window.png
 
Upvote 0
I have not found a way to set the List separator from VBA or at least it is not an easy way.


But I have 2 options, I will attach the examples:
The first is to save the csv file delimited by commas, but with extension txt, open the file, replace the commas with the new delimiter, save the file.

Code:
    sFile = "file.txt"
    ActiveWorkbook.SaveAs Filename:=sFile, FileFormat:=xlCSV
    ActiveWorkbook.Close
    Workbooks.Open sFile
    Cells.Replace What:=",", Replacement:="'"
    ActiveWorkbook.Save
    ActiveWorkbook.Close


The second, is a macro, saves data by data separated by a delimiter in a text file.

Code:
Sub Archivo()
'
    Const separador As String = "'"
    Dim ruta As String, nombre As String, fc As String
    Dim f As Long, col As Long
    Dim nFileNum As Variant
    Dim r As Range, c As Range, cadena As String
    '
    Set h1 = Sheets("Data")
    ruta = ThisWorkbook.Path & "\"
    If Left(ruta, 1) <> "\" Then ruta = ruta & "\"
    nombre = "archivo"
    '
    fc = h1.UsedRange.SpecialCells(11).Address
    f = h1.UsedRange.SpecialCells(11).Row
    col = h1.UsedRange.SpecialCells(11).Column
    nFileNum = FreeFile
    Open ruta & nombre & ".txt" For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=nFileNum]#nFileNum[/URL] 
    For Each r In h1.Range("A1:A" & f).Rows
        For Each c In h1.Range(h1.Cells(r.Row, "A"), h1.Cells(r.Row, col))
            cadena = cadena & c.Value & separador
        Next
        If cadena <> "" Then
            cadena = Left(cadena, Len(cadena) - 1)
        End If
        Print [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=nFileNum]#nFileNum[/URL] , cadena
        cadena = Empty
    Next
    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=nFileNum]#nFileNum[/URL] 
    MsgBox "End"
End Sub

They are examples for you to take and adapt them to your information.
 
Upvote 0
Dante,

I came up with something quicker as easier to do instead:

Code:
    Range("C1048576").End(xlUp).Offset(0, 1).Select
    Range(Selection, Selection.End(xlUp)).FormulaR1C1 = "=CONCATENATE(RC[-3],""~"",RC[-2],""~"",RC[-1])"
    Columns("D:D").Select
    Selection.Copy
    Sheets.Add After:=ActiveSheet
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False


    ActiveWorkbook.SaveAs ("Actual Path" & Format(Now(), "DD-MMM-YYYY") & ".csv"), FileFormat:=xlCSVWindows, CreateBackup:=False

Thanks for your effort to assist!
 
Upvote 0

Forum statistics

Threads
1,215,376
Messages
6,124,593
Members
449,174
Latest member
chandan4057

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