For/Next Loop out of control !!!

Posted on 2006-05-04
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
    LVL 29

    Accepted Solution

    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.

    LVL 9

    Expert Comment

    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

    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

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    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…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    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…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now