【问题标题】:InStr : skip the first getInStr : 跳过第一次获取
【发布时间】:2019-06-24 19:26:57
【问题描述】:

我正在使用 VBA 并使用 InStr 检查我的文件 .txt 中是否有一些模式,但我不关心我在所有文件中获得的第一个模式。我怎样才能在我得到的第一个循环中跳过所有循环。

在条目中,我有一个包含一些 .txt 文件的文件夹(包含 2000/9000 行),我有一个符号名称列表,我需要检查每个文件中是否有每个符号以及每个文件有多少个符号.这一步现在完成了,但符号文件中的第一次出现我并不感兴趣,因为它只是声明了符号,但它不是符号。因此,要计算 this.txt 文件中此类符号的数量,我必须删除我得到的第一个符号

For i = 2 To Filepath_size

    Open Filepath(i) For Input As #IndexFile
    Debug.Print "Open file: "; Filepath(i)
        Do While Not EOF(IndexFile)
        Line Input #IndexFile, LineContent
        If (InStr(LineContent, LinePrefix)) Then          
            For Each SymbolName In rngSymbo
                If (InStr(LineContent, SymbolName)) Then            
                    Set SymbolLineIndex = Range("A:A").Find(SymbolName, lookat:=xlWhole)            
                    Cells(SymbolLineIndex.Row, i + 1).Interior.ColorIndex = 4 
                    Cells(SymbolLineIndex.Row, i + 1).Value = Cells(SymbolLineIndex.Row, i + 1).Value + 1
                    Cells(SymbolLineIndex.Row, i + 1).Font.Size = 20  
                End If 
            Next SymbolName 
        End If
        Loop
Close #IndexFile
    Next i

我一周前开始使用 VBA,所以我完全不知道这是否可能,但可以跳过 InStr 发现的符号的第一次出现。目前,我的代码每次遇到符号时都会算我一次,也就是说一次比必要的次数太多,并将其显示在我的 sheet1 上的表格上

【问题讨论】:

  • 在关于使用计数器的答案之后发布...这里的另一个选项是定义您的 rngSymbo 使用 .Find() 来确定第一个实例并使用低于该值的范围。
  • 感谢 Cyril 对您的评论,但我不知道我是否能够很好地理解您,我需要整个范围的 rngSymbo。我用 rngSymbo 符号一一检查了我的一百个 .txt 文件,以查看它是否存在于此文本文件中,以及它存在于此文件中的次数。目前我的代码返回我这样的符号存在 25 次,它应该跳过它在 .txt 文件中第一次遇到这个符号并发送给我 24。

标签: vba


【解决方案1】:

添加一个计数器,然后检查计数器是否大于1

counter = 0
For i = 2 To Filepath_size

Open Filepath(i) For Input As #IndexFile
Debug.Print "Open file: "; Filepath(i)
    Do While Not EOF(IndexFile)
    Line Input #IndexFile, LineContent
    If (InStr(LineContent, LinePrefix)) Then          
        For Each SymbolName In rngSymbo
            If (InStr(LineContent, SymbolName)) Then   
                counter = counter + 1
                if counter > 1 then
                    Set SymbolLineIndex = Range("A:A").Find(SymbolName, lookat:=xlWhole)            
                    Cells(SymbolLineIndex.Row, i + 1).Interior.ColorIndex = 4 
                    Cells(SymbolLineIndex.Row, i + 1).Value = Cells(SymbolLineIndex.Row, i + 1).Value + 1
                    Cells(SymbolLineIndex.Row, i + 1).Font.Size = 20  
                end if
            End If 
        Next SymbolName 
    End If
    Loop
Close #IndexFile
Next i

【讨论】:

  • 我认为您需要为每个符号重置 counter=0,否则它会保留 hte For Each SymbolName 循环中先前符号值的值。我可能会使用counter 作为Dictionary 键入SymbolName(不改变一般流程)。否则,符号循环应该在While 循环之外,但我不确定这会不会引入其他错误。
  • 我添加了计数器,但实际上它计数到了 3025,而没有在每个符号处将其重置为 0。因此,我尝试将 counter = 0 放在“下一个 SymbolName”之前的循环中,但它仍然为 0,因此我的表中没有显示任何内容,因为它不执行其余部分。
【解决方案2】:

更新

下面的例子展示了如何计算文本中每个模式的出现次数:

Option Explicit

Sub TestSplit()

    Dim sSample As String
    Dim aSymbols()
    Dim sSymbol
    Dim a

    sSample = "abc789 def 123 defghi123 abc def 456 789 abc ghi abc 123def 123 456abc789"
    aSymbols = Array("abc", "123", "789")
    For Each sSymbol In aSymbols
        a = Split(sSample, sSymbol)
        Debug.Print "Symbol '" & sSymbol & "': " & UBound(a) & " occurances"
    Next

End Sub

初步回答

这里是一个简单的函数示例,它允许获取文本中第 n 个模式的出现位置:

Option Explicit

Sub Test()

    Debug.Print GetNthInStr("678 abc 123 abc 456", "abc", 1)
    Debug.Print GetNthInStr("678 abc 123 abc 456", "abc", 2)

End Sub

Function GetNthInStr(sText As String, sPattern As String, lNumber As Long) As Long

    Dim i As Long

    GetNthInStr = 0
    For i = 1 To lNumber
        GetNthInStr = InStr(GetNthInStr + 1, sText, sPattern)
        If GetNthInStr = 0 Then Exit For
    Next

End Function

【讨论】:

  • 感谢您的回答!我认为这个功能在我的情况下可以是完美的!但我还有一个问题。我不想知道我的符号出现在 .txt 中的哪个位置,而只是想知道它是否出现,以及它出现了多少次。例如:“678 abc 123 abc 456 abc 789 abc 123 abc 456”我的目标是知道 ABC 出现 4 次(因为我不想计算它第一次出现的时间)
猜你喜欢
  • 2013-10-03
  • 2012-08-31
  • 2011-08-08
  • 1970-01-01
  • 2021-12-26
  • 2015-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多