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

标签 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 之间有多少行,都会返回“Found it”。这是有道理的。但是,只有在找到第一个约束之前,如何强制执行第二个约束?

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

最佳答案

您之所以遇到此麻烦,是因为如果下一行不等于“电力”,则您不会重置下一行的“Bob's House”变量。因此,一旦找到鲍勃的房子,它就永远是真的,并且“电力”从哪里来并不重要。

您可以通过以下两种方式之一来实现您的目标。使用像你这样的 bool 值和“方式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

关于VBA:如果在第 1 行中找到 A,并且在第 2 行中找到 B.. 那么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15075809/

相关文章:

mysql - 如何在执行特定查询集之前检查 mysql 中的空条件?

javascript - 最后一个 else if 语句不起作用

java - java中使用readline()方法可以读取多少个字符?

java - 我如何阅读此条件并计算我的值?

string - Excel VBA - 在单元格之间复制文本并修改文本

excel - 如何有条件地合并/连接 Excel 工作表?

vba - Com DLL 在 VB6 中?

python - 可以使用 ironpython 在 Outlook 客户端拦截和重写电子邮件吗?

java - 从其他类调用代码

ipython - 在 IPython qtconsole 中启用 vi 键绑定(bind)