Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 读取特定字符串的文本文件,如果找不到,请打开msgbox_Excel_Vba - Fatal编程技术网

Excel 读取特定字符串的文本文件,如果找不到,请打开msgbox

Excel 读取特定字符串的文本文件,如果找不到,请打开msgbox,excel,vba,Excel,Vba,如何打开文本文件并查找特定字符串 我希望字符串“productactivated=true”确定是否在Userform上显示一条消息,告知用户激活 几天前,我请求帮助打开一个文本文件并进行一些阅读和写作,所以我想到了这个 Open "application.txt" For Output As #1 ClngLine = lngLine + 1 Line Input #f, strLine If InStr(1, strLine, strSearch, vbB

如何打开文本文件并查找特定字符串

我希望字符串“productactivated=true”确定是否在Userform上显示一条消息,告知用户激活

几天前,我请求帮助打开一个文本文件并进行一些阅读和写作,所以我想到了这个

Open "application.txt" For Output As #1
ClngLine = lngLine + 1
    Line Input #f, strLine
    If InStr(1, strLine, strSearch, vbBinaryCompare) > 0 Then
        MsgBox "Search string found in line " & lngLine, vbInformation
        blnFound = True
Close #1

对于您的解决方案,将使用两个文件来显示如何读取和写入文本文件。添加这篇文章只是为了向您展示如何做,但似乎并不需要您根据问题陈述给出解决方案。出于此解决方案的目的,所有文件都位于同一文件夹中

第一个文件是正在读取的文件。出于演示目的,由于未提供数据,因此使用以下数据创建,并命名为“TextFile.txt”:

第二个文件是要写入的文件。出于演示的目的,只是为了展示它是如何完成的,但根据您的问题,不需要,并命名为“TextFile.txt”:

VBA代码

This is the first line.
This is the second line and has productactivated=true.
Third line lays here.
productactivated=true is found in line four.
This is the first line.
This is the second line and has productactivated=true.
Third line lays here.
productactivated=true is found in line four.
Sub search_file()
    Const ForReading = 1, ForWriting = 2
    Dim FSO, FileIn, FileOut, strSearch, strTmp

    'FileSystemObject also called as FSO, provides an easy object based model to access computer’s file system.
    Set FSO = CreateObject("Scripting.FileSystemObject")
    'Set FileIn to the file for reading the text into the program.
    Set FileIn = FSO.OpenTextFile("TextFile.txt", ForReading)
    'Set FileOut to the file for writing the text out from the program.
        'This was added just to show "how to" write to a file.
    Set FileOut = FSO.OpenTextFile("TextFileRecordsFound.txt", ForWriting, True)
    'Set the variable to the string of text you are looking for in the file you are reading into the program.
    strSearch = "productactivated=true"

    'Do this code until you reach the end of the file.
    Do Until FileIn.AtEndOfStream
        'Store the current line of text to search to work with into the variable.
        strTmp = FileIn.ReadLine
        'Determines whether to display a message
        '(Find out if the search text is in the line of text read in from the file.)
        If InStr(1, strTmp, strSearch, vbTextCompare) > 0 Then
            'Display a message telling the user to activate.
            MsgBox strSearch & " was found in the line:" & vbNewLine & vbNewLine & strTmp, , "Activate"
            'Write the line of text to an external file, just to demo how to.
            FileOut.WriteLine strTmp
        End If
    Loop 'Repeat code inside Do Loop.

    'Close files.
    FileIn.Close
    FileOut.Close
End Sub

如果您的代码在Windows平台上运行,您可能想看看
Scripting.FileSystemObject
作为处理txt文件的替代方法;我很困惑如何使用它以及它的功能为什么检查
If Len(strTmp)>0
什么时候
If InStr
可以完成这项工作?你是对的。谢谢你的关注。我从另一个项目的代码开始,但错过了它。代码现在已更新。非常感谢@sunsetsurf提供的代码:)我已将此代码从旧代码改为新代码,它工作得非常完美:)现在一切正常!