+ Reply to Thread
Results 1 to 3 of 3

Load text file into an array

Hybrid View

  1. #1
    Registered User
    Join Date
    04-03-2019
    Location
    Nigeria
    MS-Off Ver
    2013
    Posts
    60

    Load text file into an array

    How can i load a text file into an array.
    Then, use a function to search for a text string.
    e.g.
    text search = "he came here"
    the words in a text file are "he was here yesterday when i came".
    So, I need a function that will return true when each of the words in the text search is found completely inside the text file
    Last edited by yinkajewole; 08-01-2019 at 04:08 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Load text file into an array

    Hello yinkajewole,

    This macro will return True if all of the search words are found in the text file. Case is ignored. You can interrupt the macro at anytime using the keys Ctrl+Break.

    Function AreWordsInFile(ByVal File As String, ByVal SearchWords As String) As Boolean
    
        Dim Data()      As Byte
        Dim i           As Long
        Dim n           As Long
        Dim Success     As Boolean
        Dim Text        As String
        Dim Word        As String
            
            SearchWords = SearchWords & " "
            
            Open File For Binary Access Read As #1
                ReDim Data(LOF(1))
                Get #1, , Data
            Close #1
                    
            Text = StrConv(Data, vbUnicode)
                If Text = "" Then
                    MsgBox "The file is empty.": Exit Function
                End If
            
            i = 1
            Success = True
            
            Do
                DoEvents
                n = InStr(i, SearchWords, " ")
                If n > 0 Then
                    Word = Mid(SearchWords, i, n - i)
                    i = n + 1
                    If InStr(1, Text, Word, vbTextCompare) = 0 Then
                        Success = False: Exit Do
                    End If
                End If
                If Word = "" Or i >= Len(SearchWords) Then Exit Do
            Loop
            
            AreWordsInFile = Success
            
    End Function
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    04-03-2019
    Location
    Nigeria
    MS-Off Ver
    2013
    Posts
    60

    Re: Load text file into an array

    Good one! Thanks

+ 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] Open/Load TEXT file with Specific Date and start at specific Cell
    By chergian in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-09-2017, 09:29 AM
  2. Load Values To Array Using .CurrentRegion With Array Base 0
    By NeedForExcel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-01-2016, 07:53 AM
  3. [SOLVED] Load data from text file - needed fileds in VB
    By luv2glyd in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-15-2016, 02:45 PM
  4. Load an array from ADO recorset
    By cgkmal in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 07-18-2014, 04:07 PM
  5. Macro to load text file not working.
    By calvinle in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-20-2014, 11:05 PM
  6. VBA Importing Text File makes the file longer to load everytime
    By Hudas in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-15-2014, 11:26 AM
  7. Replies: 0
    Last Post: 04-18-2005, 09:06 PM

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