Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2004-09-21
9
Medium Priority
?
866 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 375 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
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!

 

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

Industry Leaders: 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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

578 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