jinomar25
jinomar25

Reputation: 7

using delimiter as column format for impoted txt files

how can I apply delimiter "^" as columns from imported multiple txt files. Pls Help. ..

Private Sub CommandButton1_Click()


Application.ScreenUpdating = False

Dim oFileDialog As FileDialog
Dim LoopFolderPath As String
Dim oFileSystem As FileSystemObject
Dim oLoopFolder As Folder
Dim oFilePath As File
Dim oFile As TextStream
Dim RowN As Long
Dim ColN As Long
Dim iAnswer As Integer
On Error GoTo ERROR_HANDLER

Set oFileDialog = Application.FileDialog(msoFileDialogFolderPicker)

RowN = 1
ColN = 1

With oFileDialog
If .Show Then
    ActiveSheet.Columns(ColN).Cells.Clear

    LoopFolderPath = .SelectedItems(1) & "\"

    Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    Set oLoopFolder = oFileSystem.GetFolder(LoopFolderPath)

    For Each oFilePath In oLoopFolder.Files
        Set oFile = oFileSystem.OpenTextFile(oFilePath)

        With oFile

            Do Until .AtEndOfStream
                ActiveSheet.Cells(RowN, ColN).Value = .ReadLine

                LoopFolderPath = Space(1)
                RowN = RowN + 1

                Loop

            .Close
        End With
    Next oFilePath
End If
iAnswer = MsgBox("Your Textfiles have been Inputted.", vbInformation)
End With

EXIT_SUB:
Set oFilePath = Nothing
Set oLoopFolder = Nothing
Set oFileSystem = Nothing
Set oFileDialog = Nothing

Application.ScreenUpdating = True

Exit Sub

ERROR_HANDLER:
    ' Some code for error handling
    Err.Clear
    GoTo EXIT_SUB

End Sub

Upvotes: 0

Views: 118

Answers (1)

ApplePie
ApplePie

Reputation: 8942

ActiveSheet.RangE("X:X").TextToColumns Destination:=Range("X#"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, Other:=True, OtherChar:="^"

Of course you will need to replace X:X by the proper column and X# by the proper range.

It took about 30 seconds using the macro recorder to get that code.

Upvotes: 1

Related Questions