Check if two strings exist in a file

dsusercs

New Member
Joined
Mar 27, 2013
Messages
9
Hi,

I have a text file where two strings "Annotation" and "Description" may exist anywhere in the file that contains 1000+ lines. Both of these will not be on the same line for sure. Is there a way to check if they exist without getting in to a loop? If both of these are found I need to execute another command.

Thanks in advance for any help that I may get.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
I have a text file where two strings "Annotation" and "Description" may exist anywhere in the file that contains 1000+ lines. Both of these will not be on the same line for sure. Is there a way to check if they exist without getting in to a loop? If both of these are found I need to execute another command.
I am assuming you want to check the file while it is still a text file on the hard drive. You can load the entire file into memory in one-fell-swoop and then use Instr to see if your words are in the file. Something like this...
Rich (BB code):
Dim FileNum As Long
Dim TotalFile As String
FileNum = FreeFile
Open PathAndFileNameForTextFile For Binary As #FileNum
  TotalFile = Space(LOF(FileNum))
  Get #FileNum, , TotalFile
Close #FileNumIf InStr(TotalFile, "Annotation") > 0 And InStr(TotalFile, "Description") Then
  MsgBox "Both words are in the file"
Else
  MsgBox "One or more of the words is not in the file"
End If
Since the text file is in memory, you can do other things with it as needed if desired, but when you are done with the text file's contents, I would recommend setting TotalFile equal to the empty string ("") in order to free up memory before executing any other code within the procedure.
 
Upvote 0
Thanks a lot Rick. Surprising to see getting things done with very less lines of code. For the same job my code was about 25 lines and still I didn't get what I want:(.

wish you a great day!
 
Upvote 0
Rick, I was checking this and got a syntax error in the line -

Close #FileNumIf InStr(TotalFile, "Annotation") > 0 And InStr(TotalFile, "Description") Then

I trust it should be Close #FileNumIf InStr(TotalFile, "Annotation", vbBinaryCompare) > 0 And InStr(TotalFile, "Description", vbBinaryCompare) Then

Let me know if you agree.
</pre>
 
Upvote 0
Rick, I was checking this and got a syntax error in the line -

Close #FileNumIf InStr(TotalFile, "Annotation") > 0 And InStr(TotalFile, "Description") Then

I trust it should be Close #FileNumIf InStr(TotalFile, "Annotation", vbBinaryCompare) > 0 And InStr(TotalFile, "Description", vbBinaryCompare) Then
No, the Binary thing is not required... that is the default; rather, the problem is that two separate code lines somehow got combined into one line when I copy/pasted the code to my response. The above "line of code" should actually be this...

Close #FileNum
If InStr(TotalFile, "Annotation") > 0 And InStr(TotalFile, "Description") Then

Here is the whole, correctly structure code...
Rich (BB code):
Dim FileNum As Long
Dim TotalFile As String
FileNum = FreeFile
Open PathAndFileNameForTextFile For Binary As #FileNum
  TotalFile = Space(LOF(FileNum))
  Get #FileNum, , TotalFile
Close #FileNum
If InStr(TotalFile, "Annotation") > 0 And InStr(TotalFile, "Description") Then
  MsgBox "Both words are in the file"
Else
  MsgBox "One or more of the words is not in the file"
End If
 
Upvote 0

Forum statistics

Threads
1,213,538
Messages
6,114,220
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top