检查txt文件中以特定单词开头的行并计算出现的次数

时间:2013-07-29 14:23:37

标签: vba excel-vba count excel

我一直在尝试检查给定的txt文件是否包含以特定单词开头的行。然后我想要计算以该单词开头的行数。虽然我不能说得对,但我确实认为我与支票有点接近。任何和所有的帮助非常感谢! 到目前为止,这是我的代码

.Pattern = "(\nC_PIN\s)(\b\d\b\s)"
.Global = True
Set objFil3 = objFSO.OpenTextFile(inFileName)
If objFil3.IsMatch(inFileName) Then
 MsgBox "File OK"

Else: MsgBox "Wrong file type chosen. Please check directory"
End If

1 个答案:

答案 0 :(得分:1)

Robert,在对象变量上使用.ReadLine方法。

Dim numLines as Long: numLInes = 0
Dim strSearch as string '## the value you're searching for
strSearch = "Steve!"    '## MOdify as needed
Do While Not objfil3.AtEndofStream
    If Left(objfil3.ReadLine, Len(strSearch)) = strSearch Then
        numLines = numLines + 1
    Else:

    End If
Loop
    MsgBox objFil3 & " contains " & numLines & " beginning with " & strSearch