+ Reply to Thread
Results 1 to 18 of 18

Importing Data from a text file

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-15-2023
    Location
    New York, New York
    MS-Off Ver
    2019
    Posts
    115

    Importing Data from a text file

    Hi,

    I tried importing the following text file into excel.

    There is a data and time stamp; open; high; low; close; volume

    I don't know how to get excel to recognize the time stamp column.

    Can anyone help?

    Thanks,

    Ivan
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    10-05-2024
    Location
    England
    MS-Off Ver
    2021
    Posts
    95

    Re: Importing Data from a text file

    Create an empty Exel file. Select a cell where you want to download data. Then, select “Data” “from the text/CSV” tab and select your text file. In the dialog box, click the "Download" button. As a result, you get a table with your data where the dates are due to full dates.
    Attached Images Attached Images
    If you feel that I helped, can add a reputation.

  3. #3
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,404

    Re: Importing Data from a text file

    Just an observation.

    I clicked on the text file. I opened in Notepad by itself (default selection on my computer). I then selected all, copied and pasted into Excel.

    No conversion required.

  4. #4
    Forum Contributor
    Join Date
    10-15-2023
    Location
    New York, New York
    MS-Off Ver
    2019
    Posts
    115

    Re: Importing Data from a text file

    that's great that worked for you.

    That doesn't happen over here.

    I really don't know how you did that and that it actually happened since there is no file posted under your response.

  5. #5
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Importing Data from a text file

    Using q2009 instructions above, attached is the file
    Attached Files Attached Files
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  6. #6
    Forum Contributor
    Join Date
    10-15-2023
    Location
    New York, New York
    MS-Off Ver
    2019
    Posts
    115

    Re: Importing Data from a text file

    Quote Originally Posted by alansidman View Post
    Using q2009 instructions above, attached is the file
    I don't understand column 2 doesn't have a time format.

    That's the trouble I am having the same as in your excel file.

  7. #7
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Importing Data from a text file

    Should the second column be time in military format. In the original text file this cannot be determined from the data shown. You would have needed to advise us of such. Are the six digit format supposed to be hh:mm:ss format? If so that needs to be programmed. If I assumed correctly then I have formatted the second column as time in Power query before closing and loading to native excel. You can review the mcode in the file.
    Attached Files Attached Files
    Last edited by alansidman; 01-11-2025 at 07:09 PM.

  8. #8
    Forum Contributor
    Join Date
    10-15-2023
    Location
    New York, New York
    MS-Off Ver
    2019
    Posts
    115

    Re: Importing Data from a text file

    Quote Originally Posted by alansidman View Post
    Should the second column be time in military format. In the original text file this cannot be determined from the data shown. You would have needed to advise us of such. Are the six digit format supposed to be hh:mm:ss format? If so that needs to be programmed. If I assumed correctly then I have formatted the second column as time in Power query before closing and loading to native excel. You can review the mcode in the file.
    Yeah I need military time. How did you convert the column to time?

  9. #9
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,404

    Re: Importing Data from a text file

    Open the attached.

    Highlight the data / copy / paste to Excel range A1
    Attached Files Attached Files

  10. #10
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Importing Data from a text file

    Yeah I need military time. How did you convert the column to time?
    once loaded into power query I applied the following Mcode before loading and closing to Native Excel.

    = Table.TransformColumnTypes(Source,{{"Column2", type time}})

  11. #11
    Forum Contributor
    Join Date
    10-15-2023
    Location
    New York, New York
    MS-Off Ver
    2019
    Posts
    115

    Re: Importing Data from a text file

    Quote Originally Posted by alansidman View Post
    once loaded into power query I applied the following Mcode before loading and closing to Native Excel.

    = Table.TransformColumnTypes(Source,{{"Column2", type time}})
    Ok I figured out what you did.

    why is there this horrible formatting from this technique?

    How do I get rid of this formatting and just have excel data like I gyped it in?

    Does excel change this to some kind of table or something?

  12. #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.

  13. #13
    Forum Contributor
    Join Date
    10-15-2023
    Location
    New York, New York
    MS-Off Ver
    2019
    Posts
    115

    Re: Importing Data from a text file

    Quote Originally Posted by Haluk View Post
    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;
    Did you code this all by hand by yourself?

  14. #14
    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

    Quote Originally Posted by HajdukZD View Post
    Did you code this all by hand by yourself?
    Yes, I did. Pure hand made...

  15. #15
    Forum Contributor
    Join Date
    10-15-2023
    Location
    New York, New York
    MS-Off Ver
    2019
    Posts
    115

    Re: Importing Data from a text file

    You guys are advanced.....so I did it like this.

    And there is another issue I am having.

    the current data downloaded is the 1 min time frame,

    I need to convert and or adjust the data set for the 15 min, 60, and 240 minute time frames and chart it.

    any ideas on how I can accomplish this?

    Thanks
    Attached Files Attached Files

  16. #16
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Importing Data from a text file

    How do I get rid of this formatting and just have excel data like I gyped it in?
    Because it is not excel but Power Query. You can format any way you like once you bring it into native excel.

  17. #17
    Forum Contributor
    Join Date
    10-15-2023
    Location
    New York, New York
    MS-Off Ver
    2019
    Posts
    115

    Re: Importing Data from a text file

    Quote Originally Posted by alansidman View Post
    How do I get rid of this formatting and just have excel data like I gyped it in?
    Because it is not excel but Power Query. You can format any way you like once you bring it into native excel.
    It's not it changes the format of the entire workbook it's strange, even the dividing lines are gone

  18. #18
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,705

    Re: Importing Data from a text file

    It's not it changes the format of the entire workbook it's strange, even the dividing lines are gone
    I am not understanding your issue. Perhaps you can show us what you get and then mock up what you expect. Your descriptions are not helpful to diagnose and offer a solution.

+ 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 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