+ Reply to Thread
Results 1 to 8 of 8

read a line from external text files on a random basis

Hybrid View

  1. #1
    Registered User
    Join Date
    05-10-2015
    Location
    France
    MS-Off Ver
    10
    Posts
    58

    read a line from external text files on a random basis

    the random number called "RandomNum" got claculated from the total number of lines in each file. Each line is of the same exact length. I wish to read a different line each time.
    My issue is where to place that random number in my code? Every try it generate an error.

    [Code)
        Open Filename For Input As #1
        If Err <> 0 Then
            MsgBox "Not found: " & Filename, vbCritical, "ERROR"
            Exit Sub
        End If
        r = 0
        c = 0
        txt = 0
        Application.ScreenUpdating = False
            Line Input #1 Data(RandomNum)   ' get the data on a random basis from the file
            For i = 1 To 81
                Char = Mid(Data, i, 1)
                If Char <> Chr(34) Then
                    Rng.Cells(i) = Char
                End If
            Next i
        Close #1    ' #1 is the reference of the file
        Application.ScreenUpdating = True
    [/Code)]
    Last edited by Fotis1991; 05-15-2015 at 02:17 AM.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: read a line from external text files on a random basis

    Try:
    Sub test()
    Dim ff As Integer: ff = FreeFile
    Dim lines() As String, i As Long
    
    'here the code for filename and RandomNum 
    
    Open filename For Input As #ff
        lines = Split(Input(LOF(ff), #ff), vbNewLine)
    Close #ff
    
    MsgBox "line " & RandomNum & " is " & lines(RandomNum -1)
    
    'of course instead of just msgbox you can process it as you wish
    ' note it RandomNum - 1 , because lines() are numbered from 0
    
    End Sub
    PS. Please edit your post, because it does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    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
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    05-10-2015
    Location
    France
    MS-Off Ver
    10
    Posts
    58

    Re: read a line from external text files on a random basis

    Thank you for your response but between Open and Close in your example there is no RandomNum at all? only in the Msgbox.
    So how can the line be open with the RandomNum value then?

    Daniel
    I am a beginner in Excel !!
    but have already written quite much.

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: read a line from external text files on a random basis

    1. Have you tried it? It works. How? Whole file is read into array then closed. After that only needed array element is presented. It is much quicker than reading line by line.
    2. Code Tags!!! It's final kind reminder.

  5. #5
    Registered User
    Join Date
    05-10-2015
    Location
    France
    MS-Off Ver
    10
    Posts
    58

    Re: read a line from external text files on a random basis

    Oh I see but some of my files (having 81 characters + comments behind on the same line +CR) do have 20,000 lines, some may be even more. Will I be out of memory if the whole file is read in?
    I would rather read a single line each time.
    This is a game I am designing. To solve a single line the user may spend several minutes (at least 20) for a good one and over an hour for most users. So no need at all to read the whole file.
    Besides after completing one line, the user may switch to another file and pick up randomly another line there.
    Cordially,

  6. #6
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: read a line from external text files on a random basis

    1) Not really. Use square closing bracket - can't you see the difference?

    2) I don't think you tested my solution at all - IMHO shall be fine for (almost) any computer and file (20 000+ rows and 100+ characters is not a big deal - just few MB).
    So I'm a bit disappointed.
    But if you do not trust, of course you can read this "snail method" line by line.
    Something like:

    Application.ScreenUpdating = False
    dim my_Data as string, j as long
    for j=1 to RandomNum
      Line Input #1, my_Data
    next j
    For i = 1 To 81
    Char = Mid(my_Data, i, 1)
    If Char <> Chr(34) Then
    Rng.Cells(i) = Char
    End If
    Next i
    Close #1
    Shall do. In real life you shall also test if your RAndomNum is not out of the last line of the file.

  7. #7
    Registered User
    Join Date
    05-10-2015
    Location
    France
    MS-Off Ver
    10
    Posts
    58

    Re: read a line from external text files on a random basis

    Yes, it works like a charm. Looks so simple the way you did solve it !!!
    Thanks so much.

    What should I do now to close this request?

    Oh and my randomNum is generated by

    Randomize
    RandomNum = Int(FileSize * Rnd) + 1

    Is it OK?
    The Filesize is the number of lines of the file involved.

  8. #8
    Registered User
    Join Date
    05-10-2015
    Location
    France
    MS-Off Ver
    10
    Posts
    58

    Re: read a line from external text files on a random basis

    OK Thank you.
    Sorry, that was my 1st message posted.
    I did modify to comply with the rules with .

    My problem remains still unsolved.
    Thanks,

    I am designing programs for my 4 years old son to play with.

+ 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] VBA excel read random line from text file
    By VBAxxx in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-14-2017, 04:15 AM
  2. [SOLVED] IMPORTING TEXT FILES TO EXCEL - Multiple Multi-line text files, each in a new column
    By wrenchfry in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-23-2014, 08:37 PM
  3. IMPORTING TEXT FILES TO EXCELL - Multiple Multi line text files
    By Toxicca in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-12-2014, 09:56 AM
  4. Read the text in a single cell line by line.
    By moon in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-14-2010, 06:23 AM
  5. Macro to read External Files WITH Similar Names
    By thunderfoot in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-02-2005, 09:39 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