Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Aaron B asked in Computers & InternetSoftware · 1 decade ago

VBA - Search for a String in a .txt?

Ultimately, I want to create a data base of workers in a .txt file that is utilized by excel.

My (self assigned) project is to create a timesheet login program where an employee will come to the excel screen, type their name and accompanied password (stored in a seperate .txt file). I would use their input in excel to search for that string in a .txt file.

All I want to know is:

How do i search for a text string in a .txt file using VBA. What is the syntax?

Update:

barron thanks i'll check this code out.

Lots of new commands in there for me to learn.

I only know like 10-15 right now

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Dim path as String = "C:\blah\"

    Dim file_name as String = "foo.txt"

    Dim found as Boolean = false

    Dim yourstring as String = "blah"

    Dim sLine as String = ""

    Dim sr As StreamReader = File.OpenText(path & file_name)

    Do Until sr.EndOfStream

    sLine = sr.ReadLine

    if sLine.equals(yourstring) then

    found = true

    Loop

  • diep
    Lv 4
    5 years ago

    Vba Search String

Still have questions? Get your answers by asking now.