Hello new2vb,
Welcome to the Forum!
This macro will create two text files from the specified columns. You will need to change the file path, file names, worksheet name and starting range to match what you will be using.
Sub SplitDataIntoFiles()
Dim FileName1 As String
Dim FileName2 As String
Dim FilePath As String
Dim FSO As Object
Dim Rng As Range
Dim TextFile1 As Object
Dim TextFile2 As Object
Dim Wks As Object
FilePath = "C:\Test Folder"
FileName1 = "Test File 1.txt"
FileName2 = "Test File 2.txt"
Set Wks = Worksheets("Sheet1")
Set Rng = Wks.Range("A1:D1")
Set Rng = Rng.Resize(RowSize:=Wks.UsedRange.Rows.Count - Rng.Row + 1)
Set FSO = CreateObject("Scripting.FileSystemObject")
FilePath = IIf(Right(FilePath, 1) <> "\", FilePath & "\", FilePath)
FileName1 = FilePath & FileName1
FileName2 = FilePath & FileName2
Set TextFile1 = FSO.OpenTextFile(FileName1, 2, True, False)
Set TextFile2 = FSO.OpenTextFile(FileName2, 2, True, False)
For R = 1 To Rng.Rows.Count
TextFile1.WriteLine Rng.Item(R, 1) & vbTab & Rng.Item(R, 4)
TextFile2.WriteLine Rng.Item(R, 3)
Next R
TextFile1.Close
TextFile2.Close
End Sub
Adding the Macro- Copy the macro above pressing the keys CTRL+C
- Open your workbook
- Press the keys ALT+F11 to open the Visual Basic Editor
- Press the keys ALT+I to activate the Insert menu
- Press M to insert a Standard Module
- Paste the code by pressing the keys CTRL+V
- Make any custom changes to the macro if needed at this time.
- Save the Macro by pressing the keys CTRL+S
- Press the keys ALT+Q to exit the Editor, and return to Excel.
To Run the Macro...
To run the macro from Excel, open the workbook, and press ALT+F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
Bookmarks