Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tryign to export all rows of an excel file to individual txt files

I have the below code which works fine and exports the files with the rownumber as the file name. I would like to have the value of the first column be the filename and not a row number. I am at a loss for how to do this.

Sub ExportRowsAsTextFiles()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim rowNum As Long
    Dim rowRange As Range
    Dim cellValue As String
    Dim filePath As String
    Dim cell As Range
    
    Set ws = ThisWorkbook.ActiveSheet ' Change to the appropriate sheet if needed
    lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row ' Assumes data starts in column A
    
    ' Loop through each row
    For rowNum = 1 To lastRow
        ' Set the range for the current row
        Set rowRange = ws.Range("A" & rowNum & ":" & ws.Cells(rowNum, ws.Columns.Count).End(xlToLeft).Address)
        
        ' Initialize cellValue as an empty string
        cellValue = ""
        ' Loop through each cell in the row
        For Each cell In rowRange
            ' Check the data type of the cell value
            Select Case True
                Case IsNumeric(cell.Value) ' Numeric value
                    cellValue = cellValue & CStr(cell.Value) & ","
                Case IsDate(cell.Value) ' Date value
                    cellValue = cellValue & Format(cell.Value, "dd-mm-yyyy") & ","
                Case Else ' Text value or other types
                    cellValue = cellValue & CStr(cell.Value) & ","
            End Select
        Next cell
        
        ' Remove the trailing comma
        cellValue = Left(cellValue, Len(cellValue) - 1)
        
        ' Define the file path for the text file (change as needed)
        filePath = "C:\excel-export\file_" & rowNum & ".txt"
        
        ' Export the row as a text file
        Open filePath For Output As #1
        Print #1, cellValue
        Close #1
    Next rowNum
    MsgBox "Rows exported to individual text files."
End Sub

I have tried adding another variable but i am not sure what value pulls in the current row its working with and just the 1st column

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you want the value of the first column to be the filename. I would defines the filePath variable to use the value from the column (A) as below.
Replace the your Original with New code.

Original code:
' Define the file path for the text file (change as needed)
filePath = "C:\excel-export\file_" & rowNum & ".txt"

New code:
' value in column A as the filename
Dim fileName As String
fileName = ws.Cells(rowNum, 1).Value

' Array of illegal characters
Dim illegalChars() As Variant
illegalChars = Array("\", "/", ":", "*", "?", """", "<", ">", "|")

' Clean up the filename 
Dim char As Variant
For Each char In illegalChars
    fileName = Replace(fileName, char, "")
Next char

' path for the text file 
filePath = "C:\excel-export\" & fileName & ".txt"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading