+ Reply to Thread
Results 1 to 5 of 5

Problem with import of txt file

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-03-2008
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2021
    Posts
    351

    Problem with import of txt file

    I have got this code, but when I run it it does not work. The problem can be I use Fixed width, when I import the file
    Gives error on

              wrarr = Split(rearr(rec), delim)
    All I need is to import the file to where I need it.

    Sub ReadTxtFile()
    
    Dim ws As Worksheet
    Dim rearr(), wrarr()
    Dim fName As String
    Dim rowno As Long, colno As Long, rec As Long
    Dim cnt As Long, cnt2 As Long
    Dim delim As String
    
    Set ws = Worksheets("UnixO")
    fName = "C:\Do_It\test\UnixO Tapes.txt"
    
    delim = ","
    ifnum = FreeFile
    
    rowno = 1  'row 1
    colno = 1  'col A
    
        With ws
            Open fName For Input Access Read As #ifnum
                rec = 0
                    Do While Not EOF(ifnum)
                        Line Input #ifnum, tmpvar
                        rec = rec + 1
                        
                        ReDim Preserve rearr(1 To rec)
                        rearr(rec) = tmpvar
                        
                        wrarr = Split(rearr(rec), delim)
                        cnt2 = UBound(wrarr)
                            
                            For cnt = 0 To cnt2
                                ws.Cells(rowno, colno + cnt) = wrarr(cnt)
                            Next cnt
                         rowno = rowno + 1
                    Loop
                Close #ifnum
        End With
    End Sub
    I do have cross links for this.

    https://www.mrexcel.com/forum/excel-...ml#post4870883
    http://www.ozgrid.com/forum/showthre...goto=newpost**
    https://www.excelguru.ca/forums/show...rt-of-txt-file
    Last edited by hendrikbez; 07-19-2017 at 05:13 AM.

  2. #2
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Problem with import of txt file

    Hello
    Can you post the text file sample ... ?
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  3. #3
    Forum Contributor
    Join Date
    09-03-2008
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2021
    Posts
    351

    Re: Problem with import of txt file

    Here is the text file
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    09-03-2008
    Location
    Johannesburg, South Africa
    MS-Off Ver
    2021
    Posts
    351

    Re: Problem with import of txt file

    looks like no one can help me with this at all.
    any idea to do this

  5. #5
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Problem with import of txt file

    As you mention yourself, the file uses fixed width fields but the code you posted is for a file where the fields are separated with a delimiter -
    delim = ","
    You can get basic code to import a Fixed Width file by using the macro recorder and then manually importing a file. Alternatively there are plenty of code samples to import FW files available with a simple search.

+ 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. Problem with using VBA to import comma separated text file to worksheet - Excel 2013
    By tommagpieconsultants in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-04-2015, 04:57 AM
  2. File import - select file and import sheet(1) from closed workbook
    By Armitage2k in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-22-2011, 03:15 AM
  3. import file from another sheet - location problem
    By j_r_m_c in forum Excel General
    Replies: 1
    Last Post: 02-19-2011, 03:38 PM
  4. import file problem
    By naaitie in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-31-2007, 04:42 PM
  5. [SOLVED] Text Import Wizard: File Origin Problem
    By Baldbloke in forum Excel General
    Replies: 0
    Last Post: 03-29-2006, 09:55 AM
  6. [SOLVED] Import text file problem
    By condotta@shaw.ca in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-15-2005, 12:45 PM
  7. [SOLVED] problem import text file
    By Lynn in forum Excel General
    Replies: 1
    Last Post: 10-02-2005, 06:05 AM

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