Results 1 to 2 of 2

Thread: Vba import text files

  1. #1
    Join Date
    Jan 2009
    Posts
    91

    Vba import text files

    Hi friends,

    I am facing an issue with VBA.I want to import some of text files in VBA. I have heard from my friends that it's possible but i don't know how to do it.

    Can anyone help me out with this issue.

    Intel Core 2 Duo 2.8 Ghz
    915 MSI Motherboard with Intel chipset
    1gb RAM

    200gb HDD

  2. #2
    Join Date
    Dec 2008
    Posts
    79

    Re: Vba import text files

    Try to use the following code.

    Code:
    Sub GetTextFile()
       
        Dim sFile As String
        Dim sInput As String
        Dim lFNum As Long
        Dim vaFields As Variant
        Dim i As Long
        Dim lRow As Long
        Dim vaStrip As Variant
       
        Const sDELIM = "^" 'Set the delimeter
       
        lFNum = FreeFile
        sFile = "C:CaratDelim.txt"
        vaStrip = Array(vbLf, vbTab) 'list the text to strip
       
        'Open the file
        Open sFile For Input As lFNum
       
        'Loop through the file until the end
        Do While Not EOF(lFNum)
            Line Input #lFNum, sInput 'input the current line
           
            'remove the unwanted text
            For i = LBound(vaStrip) To UBound(vaStrip)
                sInput = Replace(sInput, vaStrip(i), "")
            Next i
           
            'split the text based on the delimeter
            vaFields = Split(sInput, sDELIM)
            lRow = lRow + 1
           
            'Write to the worksheet
            For i = 0 To UBound(vaFields)
                Sheet1.Cells(lRow, i + 1).Value = vaFields(i)
            Next i
        Loop
       
        Close lFNum
       
    End Sub

Similar Threads

  1. Import a text file in JTextPane
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 22-01-2010, 11:17 AM
  2. How to Import all text data in Excel spreadsheet
    By racer in forum Tips & Tweaks
    Replies: 1
    Last Post: 06-01-2010, 10:38 PM
  3. import text file into ms access
    By Clay Aiken in forum Windows Software
    Replies: 2
    Last Post: 23-06-2009, 08:30 PM
  4. How to import Text into Access through VBA
    By appueye in forum Software Development
    Replies: 2
    Last Post: 17-04-2009, 11:10 PM
  5. Replies: 5
    Last Post: 22-12-2007, 07:52 AM

Tags for this Thread

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
  •  
Page generated in 1,711,724,441.36600 seconds with 17 queries