Here's the full version I use with the choice to pick one file or every file in a folder. If you use the latter approach then you could automate the process by cleaning your data and saving it back to the original text file (or a new file).
Option Explicit
Dim myFile, myFolder
Sub test()
Dim fso As Object, fPath As String
Dim Import As Integer
Import = 1
' Import File(s)
Select Case Import
Case 1
With Application
myFile = .GetOpenFilename("Text Files,*.txt")
.ScreenUpdating = True ' Allow pop-up to disappear
.ScreenUpdating = False
End With
If myFile = "False" Then Exit Sub Else Application.Run "CleanMyData"
Case 2
Set fso = CreateObject("Scripting.FileSystemObject")
With Application
With .FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
If .SelectedItems.Count > 0 Then fPath = .SelectedItems(1) & "\" Else Exit Sub
End With
.ScreenUpdating = True ' Allow pop-up to disappear
.ScreenUpdating = False
End With
Set myFolder = fso.GetFolder(fPath).Files
For Each myFile In myFolder
If LCase(myFile) Like "*.txt" Then Application.Run "CleanMyData"
Next myFile
End Select
End Sub
Private Sub CleanMyData()
Dim sq
Open myFile For Input As #1
sq = Split(Input(LOF(1), #1), vbCrLf)
Close #1
ActiveSheet.Cells(1).End(xlUp).Resize(UBound(sq) + 1) = Application.Transpose(sq)
ActiveSheet.Columns(1).TextToColumns , , , , False, False, False, False, True, "|"
End Sub
Bookmarks