0

What do I have:

  • .csv file
  • with multiple columns
  • a list of keywords in the first column

What do I want to do:

  • add a new first column (moving the others on the right) that I'll call "A"
  • read each word of the (now) second column that I'll call "B"
  • for each word of column B I want to write an elaborated word in column A

I'll take care of the elaboration functions.

May you help me with this .csv editing?

2
  • What have you tried? Commented Mar 14, 2013 at 18:21
  • As CSV files aren't strictly formatted, adding a "column" just involves reading the entire file a line at a time, prepending the new column data and a , then writing back out again. Commented Mar 15, 2013 at 14:06

3 Answers 3

1

You have a first row of keywords:

"B,C,D,E"

but you want to insert "A" at the beginning, for instance, to have

"A,B,C,D,E"

You can read the entire line, and split on comma (,). Then rebuild the string in a loop, with a special case for the index in which you want to inert the new keyword.

Dim keywords As String
Dim newKeywords As String
Dim arr() As String
Dim keywordToInsert As String
Dim i As Integer

keywords = "B,C,D,E"
keywordToInsert = "A"

arr = Split(keywords, ",")

For i = 0 To UBound(arr) Step 1
    If i = 0 Then
        newKeywords = keywordToInsert
    End If
    newKeywords = newKeywords & "," & arr(i)
Next i

This gives you your keywords. Using this loop, you should be able to come up with your own loop to read through each additional row and operate on the first column like this.

Sign up to request clarification or add additional context in comments.

3 Comments

is it possible to directly convert the csv in an xls file ?
I suppose you could write some VBA in a Excel workbook to do this. Just use the same code, and output the cells into a new worksheet.
@maurizio You should be able to open a csv file in Excel with VB6, then do all the necessary operations on your columns.
0

This assumes you know the columns in the CSV file:

Private Sub RewriteFile(ByRef the_sFileName As String)

    Dim sFileNameTemp   As String
    Dim iFileNoInput    As Integer
    Dim iFileNoOutput   As Integer
    Dim iField1         As Integer
    Dim sField2         As String
    Dim datField3       As Date

    ' Temporary file name.
    sFileNameTemp = the_sFileName & ".tmp"

    iFileNoInput = FreeFile
    Open the_sFileName For Input As #iFileNoInput

    iFileNoOutput = FreeFile
    Open sFileNameTemp For Output As #iFileNoOutput

    Do Until EOF(iFileNoInput)
        ' Read in each field of each line into a separate variable, then write to temporary file together with new field. (If you don't know a field's type, use Variants)
        Input #iFileNoInput, iField1, sField2, datField3
        Write #iFileNoOutput, Elaborate(iField1, sField2), iField1, sField2, datField3
    Loop

    Close #iFileNoOutput
    Close #iFileNoInput

    ' Delete the original file, and replace with new file.
    Kill the_sFileName
    Name sFileNameTemp As the_sFileName

End Sub

Comments

0

Or I might upload a raw csv file into a datatable, visualize it in a datagrid in the form and then add a column to the datatable directly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.