Read comma delimited text flie into vba variables

Posted on 2009-02-16
Last Modified: 2013-11-27
Using VBA 2000, I need to read a comma seperated text file "C:\Textfile.txt".
It will always have only 1 line.
It could have 2 to 6 values at anytime.
I've attached the code that I've been trying. It is reading the file and storing the line of csv's into sTemp, but I haven't got "Split" to work. Not sure if I just don't know how to use it or if I should try something else.
I need to store each value in a variable. How can I accomplish this?

Dim sLayout, sTemp As String

    Dim Value$

    Open "C:\add_slide.txt" For Input As #2


    Line Input #2, Value$


    sTemp = Value$


    sLayout = Split(sTemp, ",")

Open in new window

Question by:coperations07
    LVL 119

    Accepted Solution


    Dim sLayout, sTemp As String
        Dim Value$, j as integer
        Open "C:\add_slide.txt" For Input As #2
        Line Input #2, Value$
        sTemp = Value$
        sLayout = Split(sTemp, ",")

        for j=0 to ubound(sLayout)

              debug.print slayout(j)
        next j
    LVL 92

    Assisted Solution

    by:Patrick Matthews
    Hello coperations07,

    Please explain what you mean by "I haven't got "Split" to work".  That code should have rendered sLayout
    as a zero-based array with the values from that line of text.



    Author Comment

    Thanks guys!
    Split was working. I just didn't know how to get the data from it.
    I just added variables like this and it works perfectly.

                s1 = sLayout(0)
                s2 = sLayout(1)
                s3 = sLayout(2)
                s4 = sLayout(3)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

     Regular Expressions Microsoft Word has sophisticated search tools that can search for patterns. For example if you wanted to search for all UK phone numbers that followed a pattern of five digits, a space and then six digits you can easily do th…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now