2

我目前将我的 VBA 设置为读取文本文件(使用FileSystemObject)并查找某些字符串。这一切都很好。但是我想要实现的是让 VBA 通读文本,当它找到某个字符串 (A) 并在它下面的下一行中找到另一个字符串 (B) 时,它会做一些事情。但前提是 B 在 A 之后。

例子:

Find in the following text "Bob's House" and in the next line after that "Electricity.
Text 1: - Return False
blablabla *Bob's House* blablabla
blablabla blablabla blablabla
blabla *Electiricity* blablabla

Text 1: - Return True
blablabla *Bob's House* blablabla
blabla *Electiricity* blablabla

这是我到目前为止所拥有的:

Set fsFile = fs.OpenTextFile(FilePath, 1, False)
sLine = fsFile.ReadLine

If VBA.InStr(1, sLine, "Bobs House") > 0 Then
   checkpointHeading = True
End If
If VBA.InStr(1, sLine, "Electricity") > 0 Then
   checkpointSubheading = True
End If
If checkpointHeading = True And checkpointSubheading = True Then
   MsgBox "Found it!"
End If

Bobs House无论和之间有多少行,这都会返回“找到它” Electricity。这是有道理的。但是,只有在找到第一个约束之后,我如何强制第二个约束呢?

是否有类似sLine +1/的东西.Readline + 1(然后在第一个 if 语句中应用第二个?)。在此先感谢,

4

3 回答 3

2

您遇到了这个麻烦,因为如果该行不等于“Electricity”,您没有在下一行重置“Bob's House”变量。因此,一旦找到鲍勃的房子,它就永远是真实的,“电力”从哪里来并不重要。

您可以通过以下两种方式之一完成您的工作。使用像你一样的布尔值和“方式 1”中的代码(我已经膨胀了一点,所以它很容易理解),或者可能是一种更好的方法,你只需将当前行字符串变量设置为一个新的字符串变量循环结束时的上一行,然后在下一行检查这两个变量,就像在“方式 2”中一样。

(请注意,我保留了您的示例中的几个拼写错误,因此代码适用于该示例)。

    Sub Way1()
    Dim fs As New FileSystemObject, fsfile As Object
    Dim sLine As String
    Dim checkpointHeading As Boolean, checkpointSubheading As Boolean

    'Open file
    Set fsfile = fs.OpenTextFile("G:Test.txt", 1, False)

    'Loop through
    Do While fsfile.AtEndOfStream <> True

        sLine = fsfile.ReadLine

        If VBA.InStr(1, sLine, "Bob's House") > 0 Then
           checkpointHeading = True
        Else
           'If the line doesn't have Bob's House then check if the line before did
           If checkpointHeading Then
               'If it did then check for Electricity
               If VBA.InStr(1, sLine, "Electiricity") > 0 Then
                   'If it's found then happy days
                   checkpointSubheading = True
               Else
                   'If it's not found then reset everything
                   checkpointHeading = False: checkpointSubheading = False
               End If
           End If
        End If

        'Check if we've found it
        If checkpointHeading = True And checkpointSubheading = True Then
           MsgBox "Found it!"

           'You may want to reset here to be safe
           checkpointHeading = False:    checkpointSubheading = False
        End If

    Loop

    fsfile.Close
    Set fsfile = Nothing
    Set fs = Nothing
End Sub

更简单,更简洁的方式2:

Sub Way2()
    Dim fs As New FileSystemObject, fsfile As Object
    Dim sLine As String, sPrevLine As String

    'Open file
    Set fsfile = fs.OpenTextFile("G:Test.txt", 1, False)

    'Loop through
    Do While fsfile.AtEndOfStream <> True

        sLine = fsfile.ReadLine

        If VBA.Len(sPrevLine) > 0 Then
            If VBA.InStr(1, sPrevLine, "Bob's House") > 0 And VBA.InStr(1, sLine, "Electiricity") Then
                MsgBox "Found it!"
            End If
        End If

        'Set the current line to the previous line *at the end of the loop*
        sPrevLine = sLine
    Loop

    fsfile.Close
    Set fsfile = Nothing
    Set fs = Nothing
End Sub
于 2013-02-26T03:17:25.063 回答
0

我没有测试它,但这应该证明逻辑:

 Const filepath = "..."

 Sub test()

    Dim fs
    Dim fsFile
    Dim found As Boolean
    Dim flag As Boolean
    Dim sLine As String

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set fsFile = fs.OpenTextFile(filepath, 1, False)

    found = False
    flag = False
    Do While Not fsFile.AtEndOfStream And Not found
        sLine = fsFile.readLine
        If flag And InStr(sLine, "Electricity") Then found = True
        flag = (InStr(sLine, "Bobs House") > 0)
    Loop

    If found Then
        MsgBox sLine
    Else
        MsgBox "not found"
    End If

 End Sub

编辑:经过测试。

于 2013-02-25T20:37:33.360 回答
0

Something like this:

    sLine = fsFile.ReadLine

    If isHeading Then
        If InStr(1, sLine, "Electricity") > 0 Then
            MsgBox "Found It!"
        End If
        isHeading = False
    End If

    If InStr(1, sLine, "Bobs House") > 0 Then
        isHeading = True
    End If
于 2013-02-25T20:43:23.137 回答