I have the text I want to use as the file name in Column B, and the content I want to add in to these files in adajacent cells in Column C. The solution is probably pretty simple, but the code I have now is creating the text files with the right name without actually writing anything to the file.
I have little to no VBA experience so this code is 100% based off of something I found on this forum
Sub CreateFiles()
Dim sExportFolder, sFN
Dim rName As Range
Dim action As Range
Dim oSH As Worksheet
Dim oFS As Object
Dim oTxt As Object
'sExportFolder = path to the folder you want to export to
'oSh = The sheet where your data is stored
sExportFolder = "H:\"
Set oSH = Sheet1
Set oFS = CreateObject("Scripting.Filesystemobject")
For Each rName In oSH.UsedRange.Columns("A").Cells
Set action = rName.Offset(, 1)
'Add .txt to the article name as a file name
sFN = rName.Value & ".txt"
Set oTxt = oFS.OpenTextFile(sExportFolder & "\" & sFN, 2, True)
oTxt.WriteLine action.Value
oTxt.Close
Next
To add to my comment, here is how your code should look, if you are looking in column B for file names, and column C for values:
Please set your sheet name in the code
Sub CreateFiles()
Dim sExportFolder As String, sFN As String
Dim oSH As Worksheet
Dim rName As Range
Dim oFS As Object, oTxt As Object
sExportFolder = "H:\"
Set oSH = ThisWorkbook.Sheets("Your Sheet Name Here") '<-- set sheet name here
Set oFS = CreateObject("Scripting.Filesystemobject")
For Each rName In oSH.Range("B1:B" & oSH.Cells(Rows.Count, 2).End(xlUp).Row)
If rName.Value <> "" Then
sFN = rName.Value & ".txt"
Set oTxt = oFS.OpenTextFile(sExportFolder & sFN, 2, True)
oTxt.WriteLine rName.Offset(0, 1)
oTxt.Close
End If
Next
End Sub
Sub CreateFiles()
Dim sExportFolder, sFN
Dim rName As Range
Dim action As Range
Dim oSH As Worksheet
Dim oFS As Object
Dim oTxt As Object
'sExportFolder = path to the folder you want to export to
'oSh = The sheet where your data is stored
sExportFolder = "H:\"
Set oSH = Worksheets("Sheet1")
Set oFS = CreateObject("Scripting.Filesystemobject")
For Each rName In oSH.UsedRange.Columns("B").Cells
Set action = rName.Offset(, 1)
'Add .txt to the article name as a file name
sFN = rName.Value & ".txt"
Set oTxt = oFS.OpenTextFile(sExportFolder & "\" & sFN, 2, True)
oTxt.WriteLine action.Value
oTxt.Close
Next
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.