Results 1 to 18 of 18

Importing Data from a text file

Threaded View

  1. #12
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,185

    Re: Importing Data from a text file

    A VBA solution is given below where ADO and SQL used.

    The important point is, file name should not involve "." so I renamed the file as NQ 03-06_Last.txt

    Both the Excel file and the text file must be in the same folder.


    Sub Test()
        Dim objConn As Object, RS As Object
        Dim myFile As String, FileNum As Long, strSQL As String
        
        Const adOpenForwardOnly = 0
        Const adLockReadOnly = 1
        Const adCmdText = 1
        Const adUseClient = 3
        
        myFile = ThisWorkbook.Path & "\NQ 03-06_Last.txt"
        
        FileNum = FreeFile
        Open ThisWorkbook.Path & "\Schema.ini" For Output As #FileNum
            Print #FileNum, "[" & Dir(myFile) & "]"
            Print #FileNum, "Format=TabDelimited"
            Print #FileNum, "DateTimeFormat=dd.mm.yyyy"
            Print #FileNum, "ColNameHeader=False"
            Print #FileNum, "DecimalSymbol=."
            Print #FileNum, "TextDelimiter=None"
            Print #FileNum, "Col1=F1 DateTime"
            Print #FileNum, "Col2=F2 Long"
            Print #FileNum, "Col3=F3 Double"
            Print #FileNum, "Col4=F4 Double"
            Print #FileNum, "Col5=F5 Double"
            Print #FileNum, "Col6=F6 Double"
            Print #FileNum, "Col7=F7 Double"
        Close #FileNum
        
        Set objConn = CreateObject("ADODB.Connection")
        Set RS = CreateObject("ADODB.Recordset")
        
        #If Win64 Then
            objConn.Open "Driver=Microsoft Access Text Driver (*.txt, *.csv);" & _
                         "Dbq=" & ThisWorkbook.Path & ";Extensions=asc,csv,tab,txt;"
        #Else
            objConn.Open "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
                         "Dbq=" & ThisWorkbook.Path & ";Extensions=asc,csv,tab,txt;"
        #End If
        
        strSQL = "Select * From [" & Dir(myFile) & "]"
        RS.Open strSQL, objConn, adUseClient, adLockReadOnly, adCmdText
        
        Range("A2:G" & Rows.Count) = ""
        Range("A2").CopyFromRecordset RS
        
        Range("B2:B" & RS.RecordCount + 1).NumberFormat = "##"":""##"":""##"
        Kill ThisWorkbook.Path & "\Schema.ini"
        
        Set RS = Nothing
        objConn.Close
        Set objConn = Nothing
    End Sub

    Output is;
    Attached Images Attached Images
    Last edited by Haluk; 01-12-2025 at 10:32 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Importing data from CSV/TEXT file
    By daneca in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 10-15-2024, 12:26 PM
  2. Replies: 4
    Last Post: 02-09-2022, 05:28 PM
  3. Importing data from text file
    By oconee1985 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-16-2021, 05:35 PM
  4. [SOLVED] importing data from text file
    By askhari139 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-24-2013, 01:36 PM
  5. Importing a data from a text file - user-defined file
    By DaveSev in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-01-2013, 07:02 PM
  6. Importing data from a text file
    By jbaranski in forum Excel General
    Replies: 3
    Last Post: 08-01-2006, 10:10 AM
  7. Importing data from text file
    By tushar_johri in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-06-2006, 05:19 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