[Solved] Convert Columns to a text file and include labels in the properties or metadata

I have an excel file that I need to convert to separate text files. One text file for every row.
However, the metadata needs to include a 'label' that originates from a column in the spreadsheet

Is this possible
 
Greeting Rachel,
Here I am going to present a VBA code using which you can extract the row values from the dataset as individual txt files. and the column header are going to be included inside each of the text. It always better to provide your original dataset where you are facing issues, with your desired output also in a sample file. In that way it becomes easy for us to provide solution.
The VBA code is
Code:
Sub ExportRowsToTextFiles()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim lastCol As Long
    Dim i As Long
    Dim j As Long
    Dim txtFile As String
    Dim labelCol As Long
    Set ws = ActiveWorkbook.Worksheets("Sheet1")
    labelCol = 4
    Debug.Print ws.Columns.Count
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
    lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
    Debug.Print lastRow
    Debug.Print lastCol
    For i = 1 To lastRow
        txtFile = ws.Cells(i, labelCol).Value
        Open ThisWorkbook.Path & "\" & txtFile & ".txt" For Output As #1
        For j = 1 To lastCol
            If j < lastCol Then
                Print #1, ws.Cells(i, j).Value & ",";
            Else
                Print #1, ws.Cells(i, j).Value;
            End If
        Next j
        Close #1
    Next i
End Sub
In this code, your dataset needs to start from the (1,1) position. If your file has data in other row or column position, then you need to set the code accordingly (i,j values).
The example file also attached, running the code will create comma separated text file in the root folder of the file. With name set by the green colored label. Hope you will find this solution helpful. Please don't hesitate to contact us if you further face any issue.
 

Attachments

Online statistics

Members online
0
Guests online
2
Total visitors
2

Forum statistics

Threads
364
Messages
1,591
Members
681
Latest member
Adilita
Back
Top