A few suggestions based only on theory:
1) Declare the variables a to g as Long. As I'm told, the long variable type
is native to Excel while integers, even though they are a simpler data type,
are converted to longs anyways.
2) Streamline the contents inside the loop as much as possible. The code
inside the loop must be evaluated for each iteration which is many thousands
or even millions of times. In particular, don't do anything unnecessary to
the worksheet such as change a cell's value from within the loop. Note that,
in your code, all of the following must be evaluated (and, in the case of
A2's value executed) with each iteration:
Range("A2").Value = Crack
If Range("A2").Value = Range("A1").Value Then
With Range("a6")
.Value = "Stop: " & Now()
End With
Exit Sub
3) I replaced all of the above with the following. I suspect that comparing
one variable (memory location) to another directly rather than comparing cell
values is more efficient:
If p = pwd Then GoTo EndMsg
4) I didn't see the point of the Do...Loop so I removed it. Same for first
If...Then construct: "If Range("A2").Value < Range("A1").Value Then"
All of the above said, if you do the math, it will probably take days (!!!)
to break 7 character passwords if you don't actually know the number of
characters - i.e. you have to test all possibilities from 1 to 7 characters.
And this assumes that only lower case alphabetic characters are used. Include
numbers and upper case and it will take till eternity. And then there's
punctuation.
Regards,
Greg
Sub passwordsearch()
Dim a As Long, b As Long, c As Long
Dim d As Long, e As Long, f As Long, g As Long
Dim pwd As String, p As String
pwd = Range("A1").Value
Range("a5").Value = "Start: " & Now()
Application.ScreenUpdating = False
For a = 97 To 122: For b = 97 To 122: For c = 97 To 122
For d = 97 To 122: For e = 97 To 122: For f = 97 To 122
For g = 97 To 122
p = Chr(a) & Chr(b) & Chr(c) & Chr(d) & Chr(e) & Chr(f) & Chr(g)
If p = pwd Then GoTo EndMsg
Next: Next: Next: Next: Next: Next: Next
EndMsg:
Range("a6").Value = "Stop: " & Now()
Application.ScreenUpdating = True
End Sub
"jasonsweeney" wrote:
I am teaching myself VBA. To learn how to write lopps I have challenged
myself to do the following:
A macro that cracks a password. I have a macro below, but please let
me know if any of you can come up with a quicker way to search the
various strings (beyond the obvious of "Application.ScreenUpdating =
False").
So here is the challenge:
In the workbook:
Cell A1 contains the 7-digit password, say "eureka" (this is the
password I want the macro to find)
Cell A2 is blank (this is the cell I want the macro to test strings)
Note:
- This macro will look only for a 7-digit password, which password
shall be in lower-case letters only (the lowercase alphabet = Chr(97)
through Chr (122))
- This macro will also display the time the search begins and stops.
Here is my first try at a macro to hack the password:
_______________
Sub passwordsearch()
'
' 7 DIGIT SEARCH
'
Dim a As Integer, b As Integer, c As Integer
Dim d As Integer, e As Integer, f As Integer, g As Integer
Dim Crack As String
Range("a5").Value = "Start: " & Now()
On Error Resume Next
If Range("A2").Value < Range("A1").Value Then
Do
For a = 97 To 122: For b = 97 To 122: For c = 97 To 122
For d = 97 To 122: For e = 97 To 122: For f = 97 To 122: For g
= 97 To 122
Crack = Chr(a) & Chr(b) & Chr(c) & Chr(d) & Chr(e) & Chr(f) &
Chr(g)
Range("A2").Value = Crack
If Range("A2").Value = Range("A1").Value Then
With Range("a6")
.Value = "Stop: " & Now()
End With
Exit Sub
End If
Next: Next: Next: Next: Next: Next: Next
Loop
End If
End Sub
___________________
I launch this macro from a button on the worksheet. Any
reccomendations are appreciated!
-- Jason
--
jasonsweeney
------------------------------------------------------------------------
jasonsweeney's Profile: http://www.excelforum.com/member.php...fo&userid=5222
View this thread: http://www.excelforum.com/showthread...hreadid=393619