1

I have a source spreadsheet in Excel with 450-or-so rows. Each row has 6 columns of data, and I need to create a separate file from each row with the filename = Column A and the contents = Columns B-G with a line break between them.

For example, I'm trying this but getting an error "File not found":

Sub DataDump()

Dim X
Dim lngRow As Long
Dim StrFolder As String

StrFolder = "/Users/danielfowler/Documents/_users_text_6.16"
X = Range([a1], Cells(Rows.Count, 2).End(xlUp))
For lngRow = 1 To UBound(X)
  Open StrFolder & "\" & X(lngRow, 1) & ".txt" For Output As #1
  Write #1, X(lngRow, 2)
  Close #1
Next
End Sub

I see a half dozen questions like this already here on StackOverflow...

But every one of these solutions returns a different error for me. I'm using Excel for Mac 2011, v14.4.2.

Community
  • 1
  • 1
Daniel Fowler
  • 385
  • 7
  • 21

2 Answers2

0
Sub VBA_Print_to_a_text_file()
    Dim strFile_Path As String
    strFile_Path = "C:\temp\test.txt" ‘Change as per your test folder path
    Open strFile_Path For Output As #1
    Print #1, "This is my sample text"
    Close #1
End Sub 
josliber
  • 43,891
  • 12
  • 98
  • 133
0

This outputs a text file for each row with column A as the title and columns B to the last column as the content for each file. You can change the directory to whatever you want but currently it saves the text file(s) to the same directory as the Excel file. You can also change the file extension to whatever you want.

Sub toFile()

    Dim FilePath As String, CellData As String, LastCol As Long, LastRow As Long
    Dim Filenum As Integer

    LastCol = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Column
    LastRow = ActiveSheet.UsedRange.SpecialCells(xlCellTypeLastCell).Row

    For i = 1 To LastRow
        FilePath = Application.DefaultFilePath & "\" & Trim(ActiveSheet.Cells(i, 1).Value) & ".xpd"
        Filenum = FreeFile

        Open FilePath For Output As Filenum
        CellData = ""

        For j = 2 To LastCol
        CellData = Trim(ActiveSheet.Cells(i, j).Value)
        Write #Filenum, CellData

        Next j

        Close #Filenum

    Next i
    MsgBox ("Done")
End Sub

As for the breaks in between each line, unfortunately I'm not experienced enough to know how to do that.

Steve C.
  • 1,333
  • 3
  • 19
  • 50