VBA:如果在第1行找到A,在第2行找到B ..那么

时间:2013-02-25 20:24:00

标签: vba if-statement readline

我目前已将我的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 HouseElectricity之间有多少行,都会返回“找到它”。这是有道理的。但是,如何在第一次限制之后才强制进行第二次约束?

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

3 个答案:

答案 0 :(得分:2)

你遇到了这个麻烦,因为如果那条线不等于'电',你就不会在下一行重置'Bob's House'变量。因此,一旦找到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

答案 1 :(得分: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

编辑:经过测试。

答案 2 :(得分:0)

这样的事情:

    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