Solved

How to convert CSV file to semi-colon, or pipe delimiter file ?

Posted on 2004-09-21
9
855 Views
Last Modified: 2010-05-18
I have Excel database converted to CSV format. There are 10 columns with data, no any separator/divider between data; since Excel columns was data separators itself.
I need convert this CSV file to semi-colon (;) delimited text data file (variant A).
Then I want repeat the same task, but use pipe (I) as delimiter (variant B)
0
Comment
Question by:Sirocco
9 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 125 total points
ID: 12115892
Hi Sirocco,

Try the following sub:

Sub ChangeDelimiter()
 Dim inFile As String, vFileNum As Integer, eLine As String
 Dim oldDelim As String, newDelim As String, vFile2 As Integer, outFile As String
 inFile = "C:\temp\csvfile.csv"
 outFile = "C:\temp\semifile.txt"
 oldDelim = ","
 newDelim = ";"
 vFileNum = FreeFile()
 Open inFile For Input As #vFileNum
 vFile2 = FreeFile()
 Open outFile For Output As #vFile2
 Do While Not EOF(vFileNum)
  Line Input #vFileNum, eLine
  eLine = Replace(eLine, oldDelim, newDelim, 1, -1, 1)
  Print #vFile2, eLine
 Loop
 Close #vFileNum
 Close #vFile2
End Sub

Just set the infile/outfile/olddelim/newdelim and you should be all set!
Matt
0
 
LVL 10

Expert Comment

by:Clif
ID: 12115896
       Dim nFileIn As Integer = FreeFile()
        Dim nFileOutA As Integer = FreeFile()
        Dim nFileOutB As Integer = FreeFile()
        Dim sTemp As String
        FileOpen(nFileIn, "Test.csv", OpenMode.Input)
        FileOpen(nFileOutA, "VariantA.csv", OpenMode.Output)
        FileOpen(nFileOutB, "VariantB.csv", OpenMode.Output)
        Do Until EOF(nFileIn)
            Input(nFileIn, sTemp)
            PrintLine(nFileOutA, Replace(sTemp, ",", ";"))
            PrintLine(nFileOutB, Replace(sTemp, ",", "|"))
        Loop
        FileClose(nFileIn, nFileOutA, nFileOutB)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12116276
This approach reads and writes the entire file at once.  It does not use a second temporary file.

Regards,

Idle_Mind

Option Explicit

Private Sub Command1_Click()
    replaceInFile "C:\Documents and Settings\Michael\My Documents\1 VB Code\CSV to ListView\data.csv", ",", ";"
    ' replaceInFile "C:\Documents and Settings\Michael\My Documents\1 VB Code\CSV to ListView\data.csv", ";", "|"
End Sub

Private Sub replaceInFile(ByVal fileName As String, ByVal oldString As String, ByVal newString As String)
    Dim ff As Integer
    Dim entireFile As String
   
    If Dir(fileName) <> "" Then
        ff = FreeFile
        Open fileName For Binary Access Read As #ff
        entireFile = Input(LOF(ff), ff)
        Close #ff
   
        entireFile = Replace(entireFile, oldString, newString)
   
        ff = FreeFile
        Open fileName For Binary Access Write As #ff
        Put #ff, , entireFile
        Close #ff
    Else
        MsgBox fileName, vbCritical, "File not found"
    End If
End Sub
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:Sirocco
ID: 12123818
Hi Matt,

yes, its work. To change delimiter I changed code line to newDelim = "|".
"," meant default delimiter in CSV file ?

As to Clif and Idle_Mind versions, I fails reproduce it, I copied code into module,(change path to my file in Idle_Mind vers), but there are something incorrect, can no see macro.

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12123897
>> As to Clif and Idle_Mind versions, I fails reproduce it, I copied code into module,(change path to my file in Idle_Mind vers), but there are something incorrect, can no see macro.

If you put the replaceInFile() functin in a module, you need to change it from Private to Public.

The Command1_Click() sub is intended to be the code behind a button named Command1 on a Form:

    Private Sub Command1_Click()
        replaceInFile "C:\Documents and Settings\Michael\My Documents\1 VB Code\CSV to ListView\data.csv", ",", ";"
        ' replaceInFile "C:\Documents and Settings\Michael\My Documents\1 VB Code\CSV to ListView\data.csv", ";", "|"
    End Sub

~IM
0
 

Author Comment

by:Sirocco
ID: 12124273
Idle_Mind,

I tried place code in ThisWorkbook. Not work. It looks very abstract. Probably, something wrong there? It seems to me code need more clear and supplied with exact instructions where place it, otherwise, only the author can force it to work.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12124447
I'm sorry.  I hadn't realized you were tyring to do this from inside Excel.

Hit Alt-F11 to bring up the Visual Basic Editor.
Then on the menu, click on Insert --> UserForm.
Draw a CommandButton (from the floating Toolbox) onto your form.
Hit F7 to view the code behind the form.
Paste the code I poster previously over any code already in the form.
Hit F5 to run the UserForm.
Click the button on the form and the code will run.

~IM
0
 

Author Comment

by:Sirocco
ID: 12124729
Despite of all my efforts I cannot force it to work. Need button to be in Userform? Its much more convenient place button on toolbar.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 12125963
Then do it the same way you got the code from mvidas to work.  Here are the two subs put together:

Private Sub replaceInFile()
    Dim fileName As String
    Dim oldString As String
    Dim newString As String
    Dim ff As Integer
    Dim entireFile As String
   
    fileName = "C:\Documents and Settings\Michael\My Documents\1 VB Code\CSV to ListView\data.csv"
    oldString = ","
    newString = ";"
    If Dir(fileName) <> "" Then
        ff = FreeFile
        Open fileName For Binary Access Read As #ff
        entireFile = Input(LOF(ff), ff)
        Close #ff
   
        entireFile = Replace(entireFile, oldString, newString)
   
        ff = FreeFile
        Open fileName For Binary Access Write As #ff
        Put #ff, , entireFile
        Close #ff
    Else
        MsgBox fileName, vbCritical, "File not found"
    End If
End Sub
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now