# Issues with Infinite Loops While Reading and Writing Text

• ### Question

• This may interest anyone.

Consider this code snippet:

```Sub CollateAdjectives()
Dim strX As String
Dim SearchString, SearchChar, MyPos
SearchString = "/"   ' String to search in.
Open "C:\Users\John\Documents\Page 6 col 1.txt" For Input As #1
Open "C:\Users\John\Documents\Noun Synonyms.txt" For Output As #2
Open "C:\Users\John\Documents\Noun Concepts Numbers.txt" For Output As #3
Open "C:\Users\John\Documents\Noun Antonyms.txt" For Output As #4

Const CompOne = "/"
Const CompTwo = "//"

Do Until EOF(1)
Line Input #1, strX
If InStr(strX, "[n]") = 0 Then
Do Until InStr(1, strX, "/") 'Infinite loop
Print #2, strX
Loop
If InStr(strX, "CONCEPT") = 0 Then 'Infinite loop
Do Until InStr(1, strX, "/")
Print #3, strX
Loop
End If
If InStr(strX, "-Ant.") = 0 Then 'Infinite loop
Do Until InStr(1, strX, ";")
Print #4, strX
Loop
End If
End If
Loop
Close #4, 3, 2, 1

End Sub```

This can be like a puzzle solver for anyone who is willing to do it, but not without some of your suggestions.

Regards,

JohnDBCTX

jp

Thursday, November 10, 2016 1:36 AM

• you had mentioned that it is because of infinite loop.

but when I debug the second code then I find that your inner loop is terminated in first time and outer loop also terminates after that.

you need to use different logic for that.

example like mentioned below.

```Public Function CountChrInString(Expression As String, Character As String) As Long
Dim iResult As Long
Dim sParts() As String

sParts = Split(Expression, Character)

iResult = UBound(sParts, 1)

If (iResult = -1) Then
iResult = 0
End If

CountChrInString = iResult
Debug.Print (CountChrInString)
End Function
```

when you call the function it will return the number of occurrences of "Y" in your above mentioned text.

also I find that you are trying to print the same string in the file instead of occurrences of y.

but it did not print because the condition is not satisfied for the loop. so it is not entering in the loop.

but we know that instr function value is >0.

so just to test you can use if condition instead of do until loop.

it will check it only 1 time but it will print the data in your file.

Regards

Deepak

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

• Marked as answer by Saturday, November 12, 2016 6:38 AM
Friday, November 11, 2016 7:59 AM

### All replies

• I'll try to give some examples to the best of my knowledge.

Given the source file output:

XXXYXXXXYXXXXY

What I want to perform is to programmatically search horizontally - that is from left to right.

```Sub CollateXAndY()
Dim strX As String
Dim strMid As String
Dim SearchString, SearchChar, MyPos
SearchString = "Y"   ' String to search in.

Open "C:\Users\John\Documents\Searching from left to right.txt" For Input As #1
Open "C:\Users\John\Documents\Output One.txt" For Output As #2
Do Until EOF(1)
Input #1, strX
Do Until InStr(1, strX, SearchString, vbBinaryCompare) > 0
Print #2, strX
Loop
Loop
Close #2, 1
End Sub
```

There result was no output in File #2 although it was created.

Regards,

JohnDBCTX

jp

Friday, November 11, 2016 5:37 AM
• you had mentioned that it is because of infinite loop.

but when I debug the second code then I find that your inner loop is terminated in first time and outer loop also terminates after that.

you need to use different logic for that.

example like mentioned below.

```Public Function CountChrInString(Expression As String, Character As String) As Long
Dim iResult As Long
Dim sParts() As String

sParts = Split(Expression, Character)

iResult = UBound(sParts, 1)

If (iResult = -1) Then
iResult = 0
End If

CountChrInString = iResult
Debug.Print (CountChrInString)
End Function
```

when you call the function it will return the number of occurrences of "Y" in your above mentioned text.

also I find that you are trying to print the same string in the file instead of occurrences of y.

but it did not print because the condition is not satisfied for the loop. so it is not entering in the loop.

but we know that instr function value is >0.

so just to test you can use if condition instead of do until loop.

it will check it only 1 time but it will print the data in your file.

Regards

Deepak

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

• Marked as answer by Saturday, November 12, 2016 6:38 AM
Friday, November 11, 2016 7:59 AM
• Here if I have the data in Output.txt while reading.

XXXX/YYYY/ZZZZ/

2. Go to and open a second file for Y's

4. Go to and open a third file for Z's

7. Close all files for reading and writing.

Would I need to create one file for reading

and three files for writing - one for X's, one for Y's, and one for Z's

by using the function you have presented?

How would I need to apply using the same function, but this time return as string type?

Regards,

JohnDBCTX

jp

• Edited by Friday, November 11, 2016 6:21 PM Another Example using the same function this time return as string type
Friday, November 11, 2016 6:18 PM
• I will try to explain this further.  Here is the improved code snippet below:

```Public Function CountChrInString(Expression As String, Character As String) As String

Dim iResult As String
Dim sParts() As String

sParts = Split(Expression, Character)

iResult = UBound(sParts, 1)

If (iResult = -1) Then
iResult = 0
End If

CountChrInString = iResult
Debug.Print (CountChrInString)
End Function

Sub EntrySearch()
Dim iResult As Long
Dim strX As String

Open "C:\Users\John\Documents\Output.txt" For Input As #1 'Read the file
Open "C:\Users\John\Documents\Output One.txt" For Output As #2 'Write all X's until program reads "/"
Open "C:\Users\John\Documents\Output Two.txt" For Output As #3 'Write all Y's until program reads "/"
Open "C:\Users\John\Documents\Output Three.txt" For Output As #4 'Write all Z's until program reads "/"

Do Until EOF(1)
Input #1, iResult
Print #2, iResult
Print #3, iResult
Print #4, iResult
Loop
Close #4, 3, 2, 1

End Sub
```

Anyone is willing to help improve this code snippet, so it will create the following outputs:

In file one.txt, XXXX

In file two.txt, YYYYY

In file three.txt, ZZZZZ

Regards,

JohnDBCTX

jp

Friday, November 11, 2016 6:37 PM
• The last post was just simple logic, but, honestly I don't know how to implement this in VBA code.

What do the -1 and 0 represent in your last post? Can you explain this further?

Regards,

JohnDBCTX

jp

Saturday, November 12, 2016 2:33 AM
• Okay, here is what I am trying to perform.

Page 6 col 1 copy.txt - program reads this:

abstemious [adj] restraining behavior or appetite /abstinent

The output is supposed to perform this, conditionally:

abstinent

While in the other file, Adjectives.txt

abstemious [adj] restraining behavior or appetite /

And here is the code snippet:

```Sub EntrySearch()
Dim iResult As Long
Dim strX As String

Open "C:\Users\John\Documents\Page 6 col 1 copy.txt" For Input As #1 'Read the file
Open "C:\Users\John\Documents\Adjectives.txt" For Output As #2 'Write the file until program reads "/"
Open "C:\Users\John\Documents\Adjective Synonyms.txt" For Output As #3 'Write all Y's until program reads "/"
Open "C:\Users\John\Documents\Output Three.txt" For Output As #4 'Write all Z's until program reads "/"

Do
Input #1, strX
Do
If InStr(1, strX, "[adj]", vbBinaryCompare) > 0 Then
Print #3, strX
Exit Do
End If
Loop Until InStr(1, strX, "/", vbBinaryCompare) > 0
Loop Until EOF(1) = True
Close #4, 3, 2, 1

End Sub```

What it is supposed to perform  is exactly what I have said; however, the program execution at runtime still

creates an infinite loop.

Could I include a -1s or 0s to improve this code snippet?

Regards,

JohnDBCTX

jp

Saturday, November 12, 2016 4:44 AM
• This link refers to optimizing string functions and using them.

I would need to do some more research on truncating strings in VBA.

Then, I shall find some answers and share them with the forum.

www.aivosto.com/vbtips/stringopt.html

jp

• Edited by Saturday, November 12, 2016 6:38 AM
Saturday, November 12, 2016 6:36 AM
•  Hi JohnDBCTX,
I Test your last posted code. you had mentioned that there is infinite loop. no, when I run the code I get same output as I tested your first code. the if condition gets false and everything terminated. there is no infinite loop. if your goal is to "XXXX/YYYY/ZZZZ" divide this string from "/" and store each part in individual file then you can use string function can easily split the stings from"/". then you can store each part as an array element. then you can use simple for loop and assign the array elements to each file. this logic will work for you to achieve your requirement. no need to use nested loops. try to implement this logic in your code and let me know if some where you stuck and get error i will try to solve that error for you.

Regards

Deepak

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

Monday, November 14, 2016 8:48 AM