• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Help needed in splitting contents

vk7

Member
I have a document with two columns (A & B) having some data in it. I have a directory path (C:\Users\vk\Files) and inside this path, I would like to create files based upon the input available in the excel file. For example, if you look at A1 & B1.. A1 is the file name and B1 is the content of that file. It is not limited to 23 rows, it can be a maximum of 8K-10K too at times. Any help would be appreciated.

For example, based on the input in the excel file, file named doc1.txt should be created and it should contain the content "This is the content in document 1". Similarly the others.
 

Attachments

  • Data.xlsx
    9.5 KB · Views: 6
Hi vk,
This should do the job.
Code:
Option Explicit
Sub belle()
Dim i As Long, lastRow As Long
Dim myPathTo As String, myFileName As String
Dim fileOut As Object, myFileSystemObject As Object
myPathTo = "C:\Users\vk\Files\"
    Set myFileSystemObject = CreateObject("Scripting.FileSystemObject")
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
        For i = 1 To lastRow
            If Not IsEmpty(Cells(i, 1)) Then
                myFileName = myPathTo & Cells(i, 1) & ".txt"
                Set fileOut = myFileSystemObject.CreateTextFile(myFileName)
                fileOut.write Cells(i, 2)
                fileOut.Close
            End If
        Next
    Set myFileSystemObject = Nothing
    Set fileOut = Nothing
End Sub
 
Code:
Sub CreateDocs()

'enable Microsoft Scripting Runtime
'tools --> references --> Microsoft scripting runtime

    Dim fso As Scripting.FileSystemObject
    Dim ts As Scripting.TextStream
    Dim r As Range
   
    Set fso = New Scripting.FileSystemObject
   
    For Each r In Range("A1", Range("A1").End(xlDown))
   
        Set ts = fso.CreateTextFile("C:\Users\vk\Files\" & r.Value & ".txt")
       
        ts.Write r.Offset(0, 1).Value
       
        ts.Close
       
    Next r
   
    Set fso = Nothing
   
   
End Sub
 
Back
Top