[B]'THIS CODE CREATES A NEW TXT FILE[/B]
Sub Create_A_New_Text_File_1()
Dim sFilePath As String
Dim fileNumber As Integer
sFilePath = "Q:\TEST.txt"
[B]'Assign a unique file number[/B]
fileNumber = FreeFile
Open sFilePath For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNumber]#fileNumber[/URL]
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=fileNumber]#fileNumber[/URL]
End Sub
[B]'THIS CODE WORKS TO COPY FROM EXCEL AND PASTE TEXT INTO A TXT FILE[/B]
Sub COPY_FROM_EXCEL_AND_PASTE_TEXT_INTO_A_TXT_FILE_2()
Range("A1").Select
Dim FilePath As String
Dim CellData As String
Dim LastCol As Long
Dim LastRow As Long
Dim fso As FileSystemObject
Set fso = New FileSystemObject
Dim stream As TextStream
LastCol = ActiveSheet.UsedRange.Columns.Count
LastRow = ActiveSheet.UsedRange.Rows.Count
[B] 'Create a TextStream.[/B]
Set stream = fso.OpenTextFile("Q:\TEST.txt", ForWriting, True)
CellData = ""
For i = 1 To LastRow
For j = 1 To LastCol
CellData = (ActiveCell(i, j).Text)
stream.WriteLine CellData
Next j
Next i
stream.Close
End Sub
Sub Format_3()
[B]‘Format the column to TEXT[/B]
Columns("A:A").Select
Selection.NumberFormat = "@"
End Sub
[B]‘THIS CODE WORKS TO COPY TEXT TO EXCEL AND PASTES INTO APPLICABLE CELLS[/B]
Sub CopyTextFile_4()
Dim oFso: Set oFso = CreateObject("Scripting.FileSystemObject")
Dim oFile: Set oFile = oFso.OpenTextFile("Q:\TEST.txt", 1)
Dim lines As Variant
lines = Split(oFile.ReadAll, vbCrLf)
oFile.Close
ThisWorkbook.Sheets(1).Range("A1").Resize(UBound(lines)).Value = Application.Transpose(lines)
[B]'Delete text file[/B]
With New FileSystemObject
If .FileExists("Q:\TEST.txt") Then
.DeleteFile "Q:\TEST.txt"
End If
End With
End Sub