且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 VBA 在文本文件中搜索字符串并返回行号

更新时间:2023-02-18 20:20:10

只需将下面的 MyString 替换为您正在寻找的任何内容即可.

Just replace MyString below with whatever it is you're looking for.

   Dim myFile As String
   Dim text As String
   Dim textline As String
   Dim posIAV As Integer
   Dim Ctr as Integer
   Dim Ctr2 as Integer

   myFile = "file path"
   Ctr = 0
   Ctr2 = 0

   Open myFile For Input As #1

   Do While Not EOF(1)
   Line Input #1, textline
   text = text & textline

   ' Increment Ctr since you're on the line now
   Ctr = Ctr + 1

   ' Check the current line to see if it contains the string we're looking for
   ' If it does, set Ctr2 to be the current line number
   If textline like "*MyString*" Then
      Ctr2 = Ctr
   End If

   Loop

   Close #1

   posIAV = InStr(text, "IAVs ADDED in this release include")

   MsgBox (posIAV)