Solved

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

Posted on 2004-09-21
9
853 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

757 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

18 Experts available now in Live!

Get 1:1 Help Now