+ Reply to Thread
Results 1 to 12 of 12

Help needed in importing a text file to excel

Hybrid View

  1. #1
    Registered User
    Join Date
    04-19-2016
    Location
    Bangalore,India
    MS-Off Ver
    2013
    Posts
    34

    Help needed in importing a text file to excel

    Hi Friends,

    I have a list of paths in my excel sheet for the text files(.txt). I have to write a code that takes the path and open a new workbook and copies that text file into the new workbook and save it.

    Each text file should get copied into a separate sheets.
    Can anyone help me with this .?

    Thanks in advance.

  2. #2
    Forum Contributor kalbasiatka's Avatar
    Join Date
    02-27-2016
    Location
    Brest, Belarus
    MS-Off Ver
    2021
    Posts
    224

    Re: Help needed in importing a text file to excel

    Hi. It is necessary to see the text file and the result, save it in excel.
    To do it for me and help me it is 2 different things!
    Sorry for my english, blame Google translator

  3. #3
    Registered User
    Join Date
    04-19-2016
    Location
    Bangalore,India
    MS-Off Ver
    2013
    Posts
    34

    Re: Help needed in importing a text file to excel

    Hi kalbasiatka,

    Thanks for the reply . I have attached my a sample textfile .
    There will be similar files in the folder whose path will be present in the excel sheet.
    I need to import the complete data from the text files by opening a new excel and save it.
    One text file per sheet then i need to add next sheet to copy the next text file using vba.

    Can you please help me.
    Attached Files Attached Files

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Help needed in importing a text file to excel

    Try recording a macro when you either open one of the text files via File>Open... or import it to a sheet using Data>From Text.

    Either should generate code that can be adapted to work with your list of files.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    04-19-2016
    Location
    Bangalore,India
    MS-Off Ver
    2013
    Posts
    34

    Re: Help needed in importing a text file to excel

    Hi Norie,
    Thanks for helping. I have got an idea to solve this.

    Thanks alot.

  6. #6
    Registered User
    Join Date
    04-19-2016
    Location
    Bangalore,India
    MS-Off Ver
    2013
    Posts
    34

    Re: Help needed in importing a text file to excel

    Hi Norie,
    I tried recording the macro as you suggested but while i am trying to run the macro by modifying the code i am getting an error "Run-time error 1004:"
    "Excel cannot find the text file ".

    This was the code when i recorded the macro i edited it according to my requirement .
    Sub Macro1()
    '
    ' Macro1 Macro
    '
    
        Dim file As String
        file = "D:\New folder\New folder (2)\sampletext.txt"
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;file", Destination:=Range("$B$1"))
            '.CommandType = 0
            .Name = "sampletext"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Columns("B:B").EntireColumn.AutoFit
    End Sub


    Can you help me with this

  7. #7
    Forum Contributor kalbasiatka's Avatar
    Join Date
    02-27-2016
    Location
    Brest, Belarus
    MS-Off Ver
    2021
    Posts
    224

    Re: Help needed in importing a text file to excel

    Sub uuu()
        Dim folder_path$, file_name$
    '--------------------------------
        With Application.FileDialog(msoFileDialogFolderPicker)
            If .Show = False Then Exit Sub
            folder_path = .SelectedItems(1)
        End With
        folder_path = folder_path & IIf(Right(folder_path, 1) = Application.PathSeparator, "", Application.PathSeparator)
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
        Set sh = Sheets.Add
        
        With sh
            file_name = Dir(folder_path & "*.txt")
            Do While file_name <> ""
                a = GetDataFromText(folder_path & file_name, "utf-8", vbCrLf, " ")
                .UsedRange.EntireRow.Delete
                .Cells(1, 1).Resize(UBound(a) + 1, UBound(a, 2) + 1) = a
                .Copy
                ActiveWorkbook.SaveAs FileName:=folder_path & Replace(file_name, "txt", "xlsx"), _
                                                    FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
                ActiveWindow.Close
                file_name = Dir
            Loop
            .Delete
        End With
        
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
        
        Beep
    End Sub
    
    Function GetDataFromText(file_path$, file_charset$, row_delimeter$, column_delimeter$) As Variant
        Dim a()
        Dim rw, cl
        Dim i&, j&
        Dim txt$
    '---------------------
        With CreateObject("ADODB.Stream")
            .Charset = file_charset
            .Open
            .LoadFromFile file_path
            txt = .ReadText
            .Close
        End With
        
        rw = Split(txt, row_delimeter)
        cl = Split(rw(0), column_delimeter)
        
        ReDim a(0 To UBound(rw), 0 To UBound(cl))
        For i = 0 To UBound(a)
            cl = Split(rw(i), column_delimeter)
            For j = 0 To UBound(a, 2)
                a(i, j) = cl(j)
            Next
        Next
        
        GetDataFromText = a
    End Function

  8. #8
    Registered User
    Join Date
    04-19-2016
    Location
    Bangalore,India
    MS-Off Ver
    2013
    Posts
    34

    Re: Help needed in importing a text file to excel

    Hi kalbasiatka,

    Thanks for the reply. I tried this code this actally covert the text file into a excel file but i need to import all the text files into a new same workbook.

    Can you help me Please?

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Help needed in importing a text file to excel

    This is how you would incorporate the variable file in the code.
    Sub Macro1()
    '
    ' Macro1 Macro
    '
    
        Dim file As String
        file = "D:\New folder\New folder (2)\sampletext.txt"
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & file, Destination:=Range("$B$1"))
            '.CommandType = 0
            .Name = "sampletext"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Columns("B:B").EntireColumn.AutoFit
    End Sub

  10. #10
    Registered User
    Join Date
    04-19-2016
    Location
    Bangalore,India
    MS-Off Ver
    2013
    Posts
    34

    Re: Help needed in importing a text file to excel

    Hi Norie,
    Thanks alot it works fine.Thanks for helping me
    Last edited by chandrup23; 07-17-2016 at 04:36 AM.

  11. #11
    Forum Contributor kalbasiatka's Avatar
    Join Date
    02-27-2016
    Location
    Brest, Belarus
    MS-Off Ver
    2021
    Posts
    224

    Re: Help needed in importing a text file to excel

    Collect all text files in a book
    Sub uuu()
        Dim folder_path$, file_name$
        Dim i&
        Dim sh As Worksheet
    '--------------------------------
        With Application.FileDialog(msoFileDialogFolderPicker)
            If .Show = False Then Exit Sub
            folder_path = .SelectedItems(1)
        End With
        folder_path = folder_path & IIf(Right(folder_path, 1) = Application.PathSeparator, "", Application.PathSeparator)
        
        Application.ScreenUpdating = False
        Application.DisplayAlerts = False
        
        Set sh = Sheets.Add
        i = 1
        
        With sh
            file_name = Dir(folder_path & "*.txt")
            Do While file_name <> ""
                a = GetDataFromText(folder_path & file_name, "utf-8", vbCrLf, " ")
                .Cells(i, 1).Resize(UBound(a), UBound(a, 2)) = a
                i = i + UBound(a)
                file_name = Dir
            Loop
            .Copy
            ActiveWorkbook.SaveAs Filename:=folder_path & "New File.xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            ActiveWindow.Close
            .Delete
        End With
        
        Application.DisplayAlerts = True
        Application.ScreenUpdating = True
        
        Beep
    End Sub
    
    Function GetDataFromText(file_path$, file_charset$, row_delimeter$, column_delimeter$) As Variant
        Dim a()
        Dim rw, cl
        Dim i&, j&
        Dim txt$
    '---------------------
        With CreateObject("ADODB.Stream")
            .Charset = file_charset
            .Open
            .LoadFromFile file_path
            txt = .ReadText
            .Close
        End With
        
        rw = Split(txt, row_delimeter)
        cl = Split(rw(0), column_delimeter)
        
        ReDim a(1 To UBound(rw) + 1, 1 To UBound(cl) + 1)
        For i = 1 To UBound(a)
            cl = Split(rw(i - 1), column_delimeter)
            For j = 1 To UBound(a, 2)
                a(i, j) = cl(j - 1)
            Next
        Next
        
        GetDataFromText = a
    End Function

  12. #12
    Registered User
    Join Date
    04-19-2016
    Location
    Bangalore,India
    MS-Off Ver
    2013
    Posts
    34

    Re: Help needed in importing a text file to excel

    Hi kalbasiatka,
    Thanks alot for helping me.The code works fine.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] importing text file into excel file using macro
    By ananthbulusu in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-26-2013, 04:08 PM
  2. Importing text file into excel
    By sentinela in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 06-02-2009, 04:20 PM
  3. Importing a text file into Excel
    By JibrilBenes in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 02-18-2009, 05:31 PM
  4. Importing text file into Excel
    By steadfast199 in forum Excel General
    Replies: 1
    Last Post: 01-27-2009, 01:16 PM
  5. importing text file to EXCEL
    By avveerkar in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-21-2007, 03:25 PM
  6. Importing text file to Excel
    By zodiac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-25-2006, 10:23 AM
  7. [SOLVED] Importing Text File into Excel
    By Martin in forum Excel General
    Replies: 1
    Last Post: 04-02-2006, 09:55 AM
  8. [SOLVED] Importing text file to excel
    By dany04 in forum Excel General
    Replies: 1
    Last Post: 11-08-2005, 09:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1