Create text Files from every row in an Excel spreadsheet

后端 未结 4 1826
死守一世寂寞
死守一世寂寞 2020-12-01 20:29

I need help creating separate text files from each row in an excel spread sheet called \"worksheet\". I want the text files to be named with content of Column A, with colum

4条回答
  •  醉梦人生
    2020-12-01 20:41

    The attached VBA macro will do it, saving the txt files in C:\Temp\

    Sub WriteTotxt()
    
    Const forReading = 1, forAppending = 3, fsoForWriting = 2
    Dim fs, objTextStream, sText As String
    Dim lLastRow As Long, lRowLoop As Long, lLastCol As Long, lColLoop As Long
    
    lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    For lRowLoop = 1 To lLastRow
    
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set objTextStream = fs.opentextfile("c:\temp\" & Cells(lRowLoop, 1) & ".txt", fsoForWriting, True)
    
        sText = ""
    
        For lColLoop = 1 To 7
            sText = sText & Cells(lRowLoop, lColLoop) & Chr(10) & Chr(10)
        Next lColLoop
    
        objTextStream.writeline (Left(sText, Len(sText) - 1))
    
    
        objTextStream.Close
        Set objTextStream = Nothing
        Set fs = Nothing
    
    Next lRowLoop
    
    End Sub
    

提交回复
热议问题