1

I have a set of passwords and I need to verify for each one if it contains 1 number, 1 upper case, 1 lower case and if the length is 8 characters.

I made this code but it isn't working for the characters, only for the length. Can you help me, please? Thanks!!

Sub Password()

    Dim b As Integer
    Dim i As Integer, j As Integer, k As Integer
    Dim psw As String

    Dim LengthOFPasswordsList As Long

    LengthOFPasswordsList = Range("D" & Rows.Count).End(xlUp).Row

    For b = 3 To LengthOFPasswordsList
        psw = Range("D" & b)

        If i >= 65 Or i <= 90 Then
            If j >= 97 Or j <= 122 Then
                If k > 48 Or k <= 57 Then
                    If psw <> (Chr(i) & Chr(j) & Chr(k) & Chr(i Or j Or k) _
                    & Chr(i Or j Or k) & Chr(i Or j Or k) & Chr(i Or j Or k)) _
                    And Len(psw) <> 8 Then
                        Range("F" & b) = "Password Inválida"
                    End If
                End If
            End If
        End If
    Next b
End Sub
6
  • 1
    For what it's worth, your If...Or statements aren't doing anything. I think you mean If...And. As it is, all values of i, j, and k will satisfy the situations. Oct 19, 2016 at 14:09
  • "If...And" doesn't work either... Oct 19, 2016 at 14:14
  • I see now what you were trying to do; you should use For loops, not If statements. Oct 19, 2016 at 14:15
  • 1
    Must read - TL;DR: password length is all that matters. Oct 19, 2016 at 14:15
  • Like that? For i = 65 To 90 For j = 97 To 122 For k = 48 To 57 Oct 19, 2016 at 14:16

3 Answers 3

2

It looked like you were trying to do too much at once. There is nothing terribly wrong with splitting up the code a bit. Many times it makes it easier to read and understand what is going on.

Sub Password()

Dim b As Integer
Dim i As Integer, j As Integer, k As Integer
Dim psw As String

Dim hasNum As Boolean, hasUpper As Boolean, hasLower As Boolean

Dim LengthOFPasswordsList As Long

LengthOFPasswordsList = Range("D" & Rows.Count).End(xlUp).Row

For b = 3 To LengthOFPasswordsList
    'assume the password is no good.
    hasNum = False
    hasUpper = False
    hasLower = False

    'capture the psw in question
    psw = Range("D" & b)

    'see if there is a number in the password
    'NOTE: the following For loops uses the ASCII values for numbers and letters.
    For k = 48 To 57
        If (InStr(1, psw, Chr(k))) Then
            hasNum = True
            Exit For
        End If
    Next k

    'See if there is an upper case
    For i = 65 To 90
        If (InStr(1, psw, Chr(i))) Then
            hasUpper = True
            Exit For
        End If
    Next i

    'See if there is a lower case
    For j = 97 To 122
        If (InStr(1, psw, Chr(j))) Then
            hasLower = True
            Exit For
        End If
    Next j

    'See if all criteria was met
    If Not hasLower Or Not hasUpper Or Not hasNum Or (Len(psw) <> 8) Then
        Range("F" & b) = "Password Inválida"
    End If
Next b

End Sub
1
  • I'm glad it does. If you are happy with it please mark my answer as correct and close the question. Oct 19, 2016 at 14:35
2

How about something like this and use the built in functionality:

Sub PasswordCheck ()
Dim bNum as Boolean, bUpper as Boolean, bLower as Boolean
Dim lRow as Long, x as Long
Dim i as Integer
Dim sPWD as String

  lRow = Range("D" & Rows.Count).End(xlUp).Row
  For x = 1 to lRow         'Go through each Password
    bNum = False
    bUpper = False
    bLower = False
    sPWD = Range("D" & x)
    For i = 1 to Len(sPWD)  'Go through each letter
      If IsNumeric(Mid(sPWD, i, 1) Then
        bNum = True
      ElseIf Mid(sPWD, i, 1) Like "[A-Z]" Then
        bUpper = True
      ElseIf Mid(sPWD, i, 1) Like "[a-z]" Then
        bLower = True
      End If
    Next i

    If bNum And bUpper And bLower Then
      'Password is valid
    Else
      'Password is Invalid
      Range("F" & x) = "Password Inválida"
    End If
  Next x
End Sub
1

I'd personally use regular expressions for this. Just check for each of your criteria and fail the password if any of them don't match. The validation function can be as simple as this:

'Requires a reference to Microsoft VBScript Regular Expressions x.x
Private Function ValidPassword(inValue As String) As Boolean
    Dim criteria As Variant
    With New RegExp
        For Each criteria In Split(".{8},[A-Z],[a-z],[0-9]", ",")
            .Pattern = criteria
            If Not .Test(inValue) Then Exit Function
        Next
    End With
    ValidPassword = True
End Function

This also hugely simplifies the calling code:

Sub Password()
    Dim b As Integer
    For b = 3 To Range("D" & Rows.Count).End(xlUp).Row
        If Not ValidPassword(Range("D" & b)) Then
            Range("F" & b) = "Password Inválida"
        End If
    Next
End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.