Create Files From Cell Content In Excel

In this post we will see how to create files from cell content in excel.

Suppose we have a list of file names in column A, and the contents for these files in column B. Now for each file in column A, we would like to create text file, where A is the title of the file and B, is the content of the file.

Basically we are creating excel file with the value in a cell and naming the file from the value in the adjacent cell.

In this example we are creating text file using excel VBA, but we can create a variety of file types.




Below is the data which we will use to create files by cell values.

excel data showing file contents in excel cells

Step 1

Open VB editor from Developer tab in excel as shown in the figure.

Open developer tab to write VBA to write code to create files from cell contents

Step 2

Insert a module in which we will write a VBA code to create files from cell content in excel.

Insert a module in the VB editor

Step 3

Now double click the newly created module to open that and paste the below code in the module.

    Sub Create_FIles()

    Dim X

    Dim lngRow As Long

    Dim StrFolder As String

    StrFolder = “D:\Excel Website”

    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




vba code to create file name and file contents from values in cell

This vba code is creating files from the content that we have in cell, the code is iterating through all the rows and creating the file content from the value in column B and naming the text files with the name that we have in column A for each corresponding item.

You just need to modify the folder path location on the code and provide the folder path in which you would like to create files from the cell content.

Hope this helped.

 




Share The Knowledge

Random Posts