+ Reply to Thread
Results 1 to 6 of 6

Specific external data from text file

Hybrid View

  1. #1
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:2007
    Posts
    298

    Specific external data from text file

    Hello everyone,

    The code I'm using comes from my last thread: http://www.excelforum.com/excel-prog...-document.html

    I was wondering if it's possible to import specific data from the Test.txt file. The file has the data shown below.
    Is it possible to, on condition, import either the Dutch data, or the English data?

    Kind regards,

    Marco

    Code
    Dim X As Integer
    Dim YourData As String
    Dim FNum As Integer
    FNum = FreeFile()
    
    Open ActiveDocument.Path & "\Test.txt" For Input Shared As #FNum
    X = 1
    
    Do While Not EOF(FNum)
        Line Input #FNum, YourData ' Reads in each line of data
        DropdownBox.AddItem YourData ' Adds data to drop down list
        If X = 1 Then
            DropdownBox.Text = YourData 'makes first item appear in list
        End If
        X = X + 1
    Loop
    Close #FNum
    Test.txt
    Dutch:
    Maandag
    Dinsdag
    Woensdag
    Donderdag
    Vrijdag
    Zaterdag
    Zondag
    
    English:
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday

  2. #2
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Specific external data from text file

    (I assume that you need something other than the days of the week.)

    What I would do is write the whole file into a string (and close the file), then search the string for the data you want.

  3. #3
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:2007
    Posts
    298

    Re: Specific external data from text file

    You are right, I need something other than the days of the week.
    I'm not really sure how I can achieve this by that method.

    I have a form where someone has to select something in a dropdown list.
    Based on that selection there is another dropdown where data is needed from the text file.
    So for each selection, I need a different part in the text file.

    For example, the first dropdown list has the items: 1, 2, 3, 4 and 5.
    The text file contains:
    'for selection 1
    A
    B
    C
    
    'for selection 2
    D 
    E
    F
    
    etc
    If someone picks "2" in the first dropdown, I only want D, E and F, to show in the second dropdown.

    Kind regards and thanks for the help!

    Marco

  4. #4
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Specific external data from text file

    Here is how I would handle the problem. I would put both lists in a string. I would place a marker (such as a comma) between every item in the list; and another marker (such as a semicolon) between the two lists. If your text contains commas and/or semicolons then you have to use some other marker. I also like using vbCr as a marker.


    Dim sList As String
    Dim sArray() As String
    
    ' ...
    
    sArray = Split(sList, ";")
    listbox1.List = Split(sArray(0), ",")
    ' Or    
    listbox1.List = Split(sArray(1), ",")
    See: ListBox1.xlsm

  5. #5
    Forum Contributor Marco-Kun's Avatar
    Join Date
    04-14-2009
    Location
    Holland
    MS-Off Ver
    Dutch:2007
    Posts
    298

    Re: Specific external data from text file

    Thank you StevenM, working good.
    I´m trying to combine this with my code for an external file.

    I´m also trying to figure out how to add a third part to the sList.

    Option Explicit
    
    Private Sub ListBox1_Click()
        Const sList = "A,B,C,D,E,F,G;H,I,J,K,L,M,N,O,P:Q,R,S,T,U"
        Dim sArray() As String
        sArray = Split(sList, ";")
        If ListBox1.ListIndex = 0 Then
            ListBox2.List = Split(sArray(0), ",")
        Else
            ListBox2.List = Split(sArray(1), ",")
        End If
        
    End Sub
    Private Sub UserForm_Initialize()
        Const sList = "List 1;List 2;List 3"
        ListBox1.List = Split(sList, ";")
    End Sub
            ListBox2.List = Split(sArray(1), ",")
    How do I add another list?

    Thank you.

    Marco

  6. #6
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Specific external data from text file

    Do you want to add a third list to sList?

    If so, it is the same principle as before. There must be a ";" between each list, and a "," between each element in the list.

    Then after:
    sArray = Split(sList, ";")
    The first list will be sArray(0), the second will be sArray(1), and the third will be sArray(2)
    Because sArray is zero based.

    Then to add the third list to a listbox or combobox, you can:

    ListBox2.List = Split(sArray(2), ",")
    The split creates an array. Here it creates an array from the third list.

    The .List from a listbox or combobox can accept a string or variant array.

    P.S.
    Const sList = "A,B,C,D,E,F,G;H,I,J,K,L,M,N,O,P:Q,R,S,T,U;V,W,X,Y,Z"
    Last edited by StevenM; 06-21-2012 at 11:24 AM. Reason: P.S.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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