Solved

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

Posted on 2004-09-21
9
858 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

710 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