Results 1 to 4 of 4

Thread: [RESOLVED] Import txt file in excel

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2008
    Posts
    57

    Resolved [RESOLVED] Import txt file in excel

    Hello, I am trying to import a txt file into excel 2003.
    My problem is that i can't figure out how to make excel understand that the txt file is column based.
    I want to import the data exactly as i see them in txt, as a table.

    Code:
    Sub OpenFile()
    Dim A As Long, B As String, C As Variant, D As Variant, E As Variant
    Dim iRow    As Long
    Dim i As Long
    Dim Fname   As Variant
    Fname = Application.GetOpenFilename("Text Files (*.txt),*.txt", , _
             "Select Text Data File")
    If Fname = False Then Exit Sub
    Open Fname For Input As #1
    iRow = 1
    i = 1
    Do While Not EOF(1)
        Input #1, A
        Cells(1, i) = A
        'iRow = iRow + 1
        i = i + 1
    Loop
    Close 1
    End Sub
    I searched in here, but the other posts did not help me
    Attached Files Attached Files
    Last edited by manin; Oct 6th, 2009 at 07:06 AM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Import txt file in excel

    Play around with this
    Code:
    Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 10/6/2009 by XXXXXXXXXXXXXX
    '
    
    '
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\manin.txt" _
            , Destination:=Range("A1"))
            .Name = "manin"
            .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 = xlFixedWidth
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(2, 1, 1, 1)
            .TextFileFixedColumnWidths = Array(2, 8, 10)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    End Sub

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Import txt file in excel

    Try this

    vb Code:
    1. Sub InmportTextFile()
    2.     Dim FlName As String
    3.    
    4.     '~~> Replace with Actual Path and File name
    5.     FlName = "C:\MyFile.Txt"
    6.    
    7.     Workbooks.OpenText Filename:=FlName, Origin:=437, _
    8.     StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
    9.     ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
    10.     , Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), _
    11.     Array(3, 1), Array(4, 1), Array(5, 1)), TrailingMinusNumbers:=True
    12. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2008
    Posts
    57

    Re: Import txt file in excel

    thanks guys.
    both codes work, but koolsid's is a bit better

Posting Permissions

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



Click Here to Expand Forum to Full Width