I've thousands of text files that I need to search to see if they contain a particular ID number. If the text file contains that ID number, I need to output certain data. The ID number can be between 1 and 6 digits long. The code I've works if the ID number is a six digit number. But if it's shorter, the problem is that the search returns information about all text files that contain a similar number.
Example: If the ID number is 41, I get hits for all files that contain, for example, ID 41 or ID 141 or ID 4123, etc.
I've collected the data about the ID numbers that I need to check if they're contained in text files in a list in a column on a sheet in an Excel file. The code I created goes through the list of ID numbers and checks if they're included in the text files.
I'd like to know how I can find a specific ID number that is shorter than 6 digits?
Example: If the ID number is 41, I get hits for all files that contain, for example, ID 41 or ID 141 or ID 4123, etc.
I've collected the data about the ID numbers that I need to check if they're contained in text files in a list in a column on a sheet in an Excel file. The code I created goes through the list of ID numbers and checks if they're included in the text files.
VBA Code:
Dim StrFile As String
Dim strFileName As String
Dim strLine As String
Dim f As Integer
Dim lngLine As Long
Dim blnFound As Boolean
Dim sFolder As String
' Open the select folder prompt
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then ' if OK is pressed
sFolder = .SelectedItems(1)
End If
End With
If sFolder <> "" Then ' if a file was chosen
' *********************
' put your code in here
' *********************
End If
'ID number to search for
strSearch = ThisWorkbook.Sheets("Test ID").Range("A" & x)
StrFile = Dir(sFolder & "\*.txt")
Do While Len(StrFile) > 0
strFileName = sFolder & "\" & StrFile
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
Sheets("Podatki").Range("B" & y) = StrFile
Sheets("Podatki").Range("A" & y) = strSearch
Sheets("Podatki").Range("D" & y) = InStr(1, strLine, strSearch, vbBinaryCompare)
blnFound = True
Exit Do
End If
Loop
Close #f
If Not blnFound Then
MsgBox "Search string not found", vbInformation
End If
I'd like to know how I can find a specific ID number that is shorter than 6 digits?