How do I set the delimiter to save as a CSV?

J

Jeroen Hofs

How do I set the delimiter when I save a file in CSV format, if my system
settings have the delimiter set to semicolon the saved CSV will automatically
alse be semicolon delimited. I want to be able to define the delimiter when
using the SaveAs method.
 
B

BrianB

Code:
--------------------

'===============================================
'- MACRO TO CONVERT WORKSHEET TO
'- PIPE (or other) DELIMITED TEXT FILE
'- WS HAS TO BE SET UP AS A SIMPLE TABLE
'- RUN MACRO FROM THE SHEET
'- Should run on any sheet
'- Brian Baulsom April 2002
'===============================================
'-
Sub PipeDelimited()
Dim FileName As String
Dim MyRow As Long
Dim MyCol As Integer
Dim MyReturn As String
Dim ws As Worksheet
Dim LastRow As Long
Dim ColumnCount As Integer
Dim MyDelimiter As String
'--------------------------------------------
'- set the delimiter
MyDelimiter = "|"
'--------------------------------------------
Set ws = ActiveSheet
MyReturn = Chr(13)
FileName = ws.Name & ".txt"
Open FileName For Append As #1
'------------------------------
'- get number of rows
LastRow = ws.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByRows).Row
'--------------------------------------------
'- get number of columns
ColumnCount = ws.Cells.Find(What:="*", _
SearchDirection:=xlPrevious, _
SearchOrder:=xlByColumns).Column
'--------------------------------------------
'- loop through rows & columns
For MyRow = 1 To LastRow
For MyCol = 1 To ColumnCount
Print #1, ws.Cells(MyRow, MyCol).Value;
If MyCol < ColumnCount Then Print #1, MyDelimiter;
Next
Print #1, MyReturn; ' end of line Return
Next
'----------------------------------------------
Close #1
MsgBox ("Done")
End Sub

'--------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top