+ Reply to Thread
Results 1 to 8 of 8

Importing a delimited text file with different data types into an array

  1. #1
    Registered User
    Join Date
    04-19-2018
    Location
    California, US
    MS-Off Ver
    Microsoft Office Professional Plus 2010
    Posts
    2

    Importing a delimited text file with different data types into an array

    I have a text file with data like so:

    ID|TIME|NAME||PRICE|QUANTITY
    123|2010-01-10 9:00|JOE||10.00|20
    456|2010-01-10 9:20|MARY||17.00|45

    I need to consume this data in a vba code without modifying the spreadsheet I am working with. I figured I would consume it into a dynamic array, but I'm not sure how to do it with different data types.

    I have tried the following (just found on the internet)

    Please Login or Register  to view this content.
    It works, in that my data is loaded but is not delimited. I need to be able to work with some of the numbers in the file. I unfortunately cannot just load the file into a sheet, though that would be easier. Can anyone help?
    Last edited by ifthenifthen; 04-19-2018 at 02:19 PM. Reason: adding [code][/code]

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: Importing a delimited text file with different data types into an array

    Welcome to the forum, unfortunately your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    Please edit your first post so we can continue to help you.

    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Importing a delimited text file with different data types into an array

    So what's the end result you are looking for?

    If you want each delimited value in it's own array element, you need additional step.
    I'd probably do it like below.
    Please Login or Register  to view this content.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Let's play !

    Quote Originally Posted by ifthenifthen View Post
    I unfortunately cannot just load the file into a sheet, though that would be easier.
    Yes easier and of course you can in a temp worksheet or in a temp workbook !


    Quote Originally Posted by ifthenifthen View Post
    I need to be able to work with some of the numbers in the file.
    Quite same as CK76 but without last empty row if exists in the source text file,
    run the next demonstration and see in Locals window the content of the V array variable …
    Here INDEX worksheet function extracts all rows and columns from source text file
    - as we can't guess what you really need -
    but this function can extract part of rows / columns (for example rows #2 to last and columns 5 & 6).

    PHP Code: 
    Sub Demo1()
         
    Dim VW
             V 
    ThisWorkbook.Path "\test.txt"
      
    If Dir(V) = "" Then Beep: Exit Sub
        Open V 
    For Input As #9
        
    = Array(Split(Input(LOF(9), #9), vbNewLine))
        
    Close #9
        
    ReDim Preserve V(UBound(V(0)) + (V(0)(UBound(V(0))) = ""))
        For 
    UBound(VTo 0 Step -1:  V(W) = Split(V(0)(W), "|"):  Next
        W 
    Evaluate("COLUMN(" Cells(1).Resize(, UBound(V(0)) + 1).Address ")")
        
    Application.Index(VEvaluate("ROW(1:" UBound(V) + ")"), W)
        
    Stop
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  5. #5
    Registered User
    Join Date
    04-19-2018
    Location
    California, US
    MS-Off Ver
    Microsoft Office Professional Plus 2010
    Posts
    2

    Re: Importing a delimited text file with different data types into an array

    I'm sorry that I did not better explain what I am trying to do.

    Ultimately, I am creating a tool that a user can interact with. The user inputs a file path (always a text file, always the same columns, though the order the columns might be different). The user can input a "Name" like JOE, and then fetch certain data. For instance, I might include a button that returns the sum of all items in the "quantity" column associated with JOE. What I am trying to avoid is pasting the contents of a text file into a sheet so that the tool is cleaner.

    I thought that the best way to start this would be to load the text file into an array, and then figure out how to do calculations with it's contents. So far, I am having trouble even returning one line of the array I built with debug.print.

    If this is not the best way to go about this task, I would love to hear opinions!

    I am experimenting with the options given now - thank you so much.

  6. #6
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,883

    Re: Importing a delimited text file with different data types into an array

    There are few ways to accomplish your need.

    1. Use ADO to query text file and have schema file created. This allows you to construct SQL query string to retrieve desired info without need to manipulate text data.

    2. Since you have Pro Plus, leverage PowerQuery and pass parameter(s) user enter in cell(s) to query data from text file. This is probably the easiest to maintain, but will require user to hit "Refresh All" before the data is brought in and requires all users to have access to PowerQuery.

    3. Use code Marc and I posted to fill array. Then perform lookup operation on array elements using condition provided. Application.Match, Application.Index will be useful here.

    It would be helpful if you can upload couple of sample text files (with about 10~25 rows of data) and some desired results. To upload file, use "Go Advanced" button in quick reply menu and find "Manage Attachments" hyperlink. Click on it and it will launch new tab/window for managing attachments.

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this !

    Quote Originally Posted by ifthenifthen View Post
    What I am trying to avoid is pasting the contents of a text file into a sheet so that the tool is cleaner
    It' a pity a beginner avoids to work within a worksheet 'cause
    - it's the easy way as it's the purpose of Excel
    - the code is very light, efficient and easy to maintain using Excel inner features (like formula & filter)
    - the use of the (temp) worksheet can be easily hidden during execution …


    Quote Originally Posted by ifthenifthen View Post
    […] sum of all items in the "quantity" column associated with JOE.
    An array demonstration :

    PHP Code: 
    Sub Demo1JOEqty()
       Const 
    "|""QUANTITY""JOE"
         
    Dim VWD#
             
    ThisWorkbook.Path "\test.txt"
      
    If Dir(V) = "" Then Beep: Exit Sub
        Open V 
    For Input As #9
        
    Split(Input(LOF(9), #9), vbNewLine)
        
    Close #9
        
    Application.Match(KSplit(V(0), S), 0)
        If 
    IsNumeric(WThen W Else Beep: Exit Sub
        
    For Each V In Filter(VSTrue):  Val(Split(VS)(W)):  Next
        MsgBox N 
    " " " total = " DvbInformation" Text file extraction"
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 04-20-2018 at 01:10 PM. Reason: optimization …

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Importing a delimited text file with different data types into an array


    Guys, thanks for the rep' !

+ 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. Importing non-delimited text file into excel
    By Miteshkg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-23-2016, 04:48 AM
  2. Importing txt space delimited text file
    By huytran102 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-16-2014, 04:13 PM
  3. Importing Delimited Text File
    By JHCross in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-25-2011, 06:01 PM
  4. Importing Delimited Text file
    By mmb.kala in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-23-2010, 02:45 AM
  5. Importing space delimited text file
    By EDIEC in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-23-2008, 03:44 PM
  6. Replies: 0
    Last Post: 08-22-2005, 08:05 AM
  7. [SOLVED] Excel 2003 Importing large delimited text file
    By Ron in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-05-2005, 11:06 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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1