For/Next Loop out of control !!!

Posted on 2006-05-04
Medium Priority
Last Modified: 2013-12-25
Below is a sub routine which uses the For Next Loop procedure, the problem is that the Loop is not working correctly.  It appears that the same "next" statement repeats over and over, not ever ending.  How do I get this to work correctly.

Sub PasswordBreaker()

Dim i As Integer, j As Integer, k As Integer
Dim l As Integer, m As Integer, n As Integer
Dim i1 As Integer, i2 As Integer, i3 As Integer
Dim i4 As Integer, i5 As Integer, i6 As Integer
On Error Resume Next
For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126

ActiveSheet.Unprotect Chr(i) & Chr(j) & Chr(k) & _
Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
If ActiveSheet.ProtectContents = False Then
MsgBox "One usable password is" & Chr(i) & Chr(j) & _
Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _
Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)

Range(“a1”).FormulaR1C1 = Chr(i) & Chr(j) & _
Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _
Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
Exit Sub
End If
Next: Next: Next: Next: Next: Next
Next: Next: Next: Next: Next: Next

End Sub
Question by:Alexhomar
  • 2
LVL 29

Accepted Solution

leonstryker earned 500 total points
ID: 16612212
The problem with your code is that the "Exit Sub" is inside an If statement. If the true part of the condition is not met the loop will just continue. With the number of nested loops you have in your code, it is possible that it will take a very long time to finish.


Expert Comment

ID: 16612652
You know i tried the same thing in C which is 6 times faster than vb. But it took 8 hours of time to form upto only 5char password. I tried that program to break 8 char password. So, it may take another 10-20 days of time to try all the char combinations. So, only some web sites suggests/imposed the min 6 char length for passwords. Meanwhile you connected your Excel sheet too. So, it may take much more time to complete.

Author Comment

ID: 16614419
Just so that everyone knows, the nature of my question is for an Excel Worksheet I created which I protected, but unfortunately can't remember the password I gave it. Alex

Author Comment

ID: 16615126
Thanks everyone for your input.  I am a fraid that the nature of my question might be misconstrued.  I assure you that it was with good intent that possed the question.  The Code used was found on the internet by an unknown Author, thus where the sub routine name originated.  Again thanks for all your suggestions and help.  Alex

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question