[英]Display search text in text file with VBA Excel
I am new to VB and have a problem.我是 VB 新手,遇到了问题。
I have a text file named data.txt
.我有一个名为data.txt
的文本文件。 It has 1 lines in it它有 1 行
IamanewstudentHeisanewstudentthestudentinthisclassisveryfunnythisuniversityhave300studentthestudentisveryfriendlywithnewcommer IamanewstudentHeisanewstudent这个班级的学生很有趣这所大学有300个学生这个学生对新来的人非常友好
I write a script which reads this text file and look for the string such as "stutent" and print all the "student" we can found in cell in excel (B1,C1,D1....).我编写了一个脚本来读取这个文本文件并查找诸如“student”之类的字符串并打印我们可以在excel(B1,C1,D1 ....)的单元格中找到的所有“学生”。 In this example we have 5 "student".在这个例子中,我们有 5 个“学生”。 It will display in cell B1,C1,D1,E1,F1 in sheet.它将显示在工作表中的单元格 B1、C1、D1、E1、F1 中。
I tried till this point but it just give me only one "student" not five.我一直尝试到这一点,但它只给我一个“学生”而不是五个。
Sub SearchTextFile()
Const strFileName = "C:\data.txt"
Const strSearch = "student"
Dim strLine As String
Dim f As Integer
Dim lngLine As Long
Dim blnFound As Boolean
Dim lPosition As Long
f = FreeFile
Open strFileName For Input As #f
Do While Not EOF(f)
lngLine = lngLine + 1
Line Input #f, strLine
If InStr(1, strLine, strSearch, vbBinaryCompare) > 0 Then
blnFound = True
lPosition = InStr(1, strLine, strSearch, vbTextCompare)
MsgBox "Search string found" & strSearch, vbInformation
Exit Do
End If
Loop
Close #f
If Not blnFound Then
MsgBox "Search string not found", vbInformation
End If
End Sub
I would use RegEx to count the number of occurences in the line with the following function我将使用 RegEx 来计算具有以下功能的行中的出现次数
Function noInStr(line As String, pattern As String) As Long
Dim regEx As Object, matches As Object
Set regEx = CreateObject("vbscript.regexp")
With regEx
.MultiLine = False
.Global = True
.IgnoreCase = True
.pattern = pattern
End With
Set matches = regEx.Execute(line)
noInStr = matches.count
End Function
You could use it in your code like that你可以像这样在你的代码中使用它
Sub SearchTextFile()
Const strFileName = "C:\data.txt"
Const strSearch = "student"
Dim strLine As String
Dim f As Integer
Dim lngLine As Long
Dim blnFound As Boolean
Dim lPosition As Long
f = FreeFile
Open strFileName For Input As #f
Do While Not EOF(f)
lngLine = lngLine + 1
Line Input #f, strLine
Dim count As Long
count = noInStr(strLine, strSearch)
If count > 0 Then
blnFound = True
MsgBox "Search string found " & count & "- times: " & strSearch, vbInformation
Exit Do
End If
Loop
Close #f
If Not blnFound Then
MsgBox "Search string not found", vbInformation
End If
End Sub
If you also need the positions you could retrieve them with RegEx, too.如果您还需要这些职位,您也可以使用 RegEx 检索它们。
Update : This is how you could also retrieve the positions更新:这是您还可以检索位置的方法
Function colInStr(line As String, pattern As String) As Collection
Dim regEx As Object, matches As Object
Set regEx = CreateObject("vbscript.regexp")
With regEx
.MultiLine = False
.Global = True
.IgnoreCase = True
.pattern = pattern
End With
Set matches = regEx.Execute(line)
Dim col As New Collection
Dim i As Long
For i = 0 To matches.count - 1
col.Add matches(i).FirstIndex
Next i
Set colInStr = col
End Function
You also need to modify your code, below only the relevant part您还需要修改您的代码,以下仅相关部分
Dim count As Long, col As Collection
Set col = colInStr(strLine, strSearch)
count = col.count
If count > 0 Then
blnFound = True
MsgBox "Search string found " & count & "- times: " & strSearch, vbInformation
Exit Do
End If
The positions are stored in the collection.位置存储在集合中。
This will help find all the student strings and their right positions.这将有助于找到所有学生字符串及其正确位置。 I have commented my changes.我已经评论了我的更改。 I run the test using your file我使用您的文件运行测试
Sub SearchTextFile()
Const strFileName = "C:\data.txt"
Const strSearch = "student"
Const strReplaceSearch = "tneduts"
Dim strLine As String
Dim f As Integer
Dim lngLine As Long
Dim blnFound As Boolean
Dim lPosition As Long
f = FreeFile
Open strFileName For Input As #f
Do While Not EOF(f)
lngLine = lngLine + 1
Line Input #f, strLine
'' For every line retreived, loop for all occurences of student
Do While (InStr(1, strLine, strSearch, vbBinaryCompare) > 0)
blnFound = True
lPosition = InStr(1, strLine, strSearch, vbTextCompare)
MsgBox "Search string found" & strSearch, vbInformation
'' remove the string student found and search for the next, we replace the word student with tneduts, that helps us keep the lPosition right
strLine = Replace(strLine, strSearch, strReplaceSearch, 1, 1)
Loop
Loop
Close #f
If Not blnFound Then
MsgBox "Search string not found", vbInformation
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.