Hi to all,
I am hoping a kind individual may be able to help me solve this problem.
I have a macro that imports text files into excel Rows perfectly.
I am now trying to re-save these Rows as individual text files.
So that each row is saved as a text file with the original formatting.
Here is the original code used to import text files.
The whole purpose of importing text files is so that I may be able to add some additional information in other columns then re-output the new text file with original formatting - including the line breaks and spaces.
I have referenced many threads including one below to try and solve this task:
Write each Excel row to new .txt file with ColumnA as file name - Stack Overflow
I apologize for my newbie skills - but I have tried adapting code snippets as well as look at other alternatives and adapt various codes - however I am just not doing things right, due to my lack of knowledge.
If any one would be kind enough to point me in the right direction, I would be extremely grateful. As per the code I don't mind if the file name starts in column A - I am just trying to figure out how to loop over the rows to output each row to a text file.
To Summarise
I am trying to save each row as a text file with the original formatting.
I would be really grateful for any help.
Thank you so much in advance for your time and help
Shana
I am hoping a kind individual may be able to help me solve this problem.
I have a macro that imports text files into excel Rows perfectly.
I am now trying to re-save these Rows as individual text files.
So that each row is saved as a text file with the original formatting.
Here is the original code used to import text files.
Code:
Option Explicit
Sub ImportCompleteTextFileAndName()
'Imports Each Text Files into a Single cell - With Original Formatting
'Imports the File Name into Column A
Dim sPath As String
Dim iRow As Long
Dim strString As String
Dim fso As FileSystemObject
Dim xFile As File
Dim xFolder As Folder
Set fso = CreateObject("Scripting.FileSystemObject")
Set xFolder = fso.GetFolder("C:\Users\Desktop\Import\")
iRow = 2 ' Row to start inserting data
For Each xFile In xFolder.Files
If InStr(1, xFile.Name, ".txt") <> 0 Then
Dim lFile As Long
Dim szLine As String
lFile = FreeFile()
Open xFile.Path For Input As lFile
strString = ""
While Not EOF(lFile)
Line Input #lFile, szLine
' Concatenete lines from text file
strString = strString & szLine & vbCrLf
Wend
'==========================================
' Column 1 = A , 2 = B, 3 = C, 4 =D, 5 = E
' Import Text file
Cells(iRow, 5).Value = strString ' Imports text file into E
'========= Import the File Name
Cells(iRow, 4).Value = xFile.Name ' Imports the filename into Column D
iRow = iRow + 1
' Close the file
Close lFile
Application.ScreenUpdating = True
End If
Next ' End of LOOP
MsgBox "Completed!"
'Adapted From Original
'http://www.mrexcel.com/forum/excel-questions/909742-visual-basic-applications-excel-import-text-file-names-into-column.html#post4374692
'http://www.mrexcel.com/forum/excel-questions/462499-import-whole-text-file-into-single-cell.html
End Sub
The whole purpose of importing text files is so that I may be able to add some additional information in other columns then re-output the new text file with original formatting - including the line breaks and spaces.
I have referenced many threads including one below to try and solve this task:
Write each Excel row to new .txt file with ColumnA as file name - Stack Overflow
Code:
Sub SaveEachRowAsTextFile()
'Save each Row as a text file - With Original Formatting including Line breaks
Dim wb As Excel.Workbook, wbNew As Excel.Workbook
Dim wsSource As Excel.Worksheet, wsTemp As Excel.Worksheet
Dim filePath As String
Dim fileName As String
Dim rowRange As Range
Dim cell As Range
filePath = "C:\Users\Desktop\a\" '<--- Modify this for your needs.
For Each cell In Range("A2", Range("A2048576").End(xlUp))
Set rowRange = Range(cell.Address, Range(cell.Address).End(xlToRight))
fileName = filePath & cell.Offset(0, -1).Value
'
' Insert code to write the text file here
Set wbNew = ActiveWorkbook
Set wsTemp = wbNew.Worksheets(1)
wbNew.SaveAs fileName & ".txt", xlTextWindows 'save as .txt
wbNew.Close
ThisWorkbook.Activate
r = r + 1
Loop
'
' you will be able to use the variable "fileName" when exporting the file
Next
End Sub
I apologize for my newbie skills - but I have tried adapting code snippets as well as look at other alternatives and adapt various codes - however I am just not doing things right, due to my lack of knowledge.
If any one would be kind enough to point me in the right direction, I would be extremely grateful. As per the code I don't mind if the file name starts in column A - I am just trying to figure out how to loop over the rows to output each row to a text file.
To Summarise
I am trying to save each row as a text file with the original formatting.
I would be really grateful for any help.
Thank you so much in advance for your time and help
Shana