首页 > 解决方案 > 在 .txt 文件 VBA 中搜索用户提供的字符串

问题描述

我有以下代码,它返回文件夹目录中 .txt 文件的所有文件名。我想添加到此代码以打开 txt 文件并搜索用户提供的字符串,如果在文件中找到该字符串,则返回 true 或 false。

任何帮助将非常感激。

Sub VBAF1_List_All_TXT_Files_Using_Dir()

'Variable Declaration
Dim sFilePath As String
Dim sFileName As String
Dim sUserString As String

'Specify File Path
sFilePath = "C:\Users\d85307\Documents\Automation Anywhere Files\Automation Anywhere\My Docs\UK\Vehicle Delivery\ATDC\Log"
' sUserString =

'Check for back slash
If Right(sFilePath, 1) <> "\" Then
    sFilePath = sFilePath & "\"
End If
    
sFileName = Dir(sFilePath & "*.txt")

Do While Len(sFileName) > 0
    If Right(sFileName, 3) = "txt" Then
        'Display file name in immediate window
        Debug.Print sFileName
        
    End If
    'Set the fileName to the next available file
    sFileName = Dir
Loop

End Sub

标签: vbaloopstxt

解决方案


推荐阅读