Solved

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

Posted on 2004-09-21
9
861 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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 86

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 86

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 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month5 days, 10 hours left to enroll

626 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