使用 VBA 在文本文件中搜索字符串并返回行号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23369908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use VBA to search text file for string and return line number
提问by user3586080
I'm trying to search through a txt file for a certain string using a Do While Not EOF loop and then either return or just set a flag of that line number whenever the string is found.
我正在尝试使用 Do While Not EOF 循环在 txt 文件中搜索某个字符串,然后在找到该字符串时返回或仅设置该行号的标志。
I'm extremely new to VBA with access. What would be the most basic way to go about doing this? I'm using Access 2007
我对具有访问权限的 VBA 非常陌生。这样做的最基本方法是什么?我正在使用 Access 2007
Thank you
谢谢
Here is what I have so far, I pulled it from various examples online to try to get it to work.
这是我到目前为止所拥有的,我从网上的各种示例中提取它以尝试使其工作。
Dim myFile As String
Dim text As String
Dim textline As String
Dim posIAV As Integer
myFile = "file path"
Open myFile For Input As #1
Do While Not EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
posIAV = InStr(text, "IAVs ADDED in this release include")
MsgBox (posIAV)
回答by Johnny Bones
Just replace MyStringbelow with whatever it is you're looking for.
只需将下面的MyString替换为您正在寻找的任何内容。
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)