VBA Question

wkr6469

New Member
Joined
Sep 17, 2011
Messages
3
I am trying to set up a macro to evaluate a password for a VBA class i'm taking. The password needs to be 8 characters in lenght, starting with an upper case letter, and must consist entirely or upper case letters or numbers besides that. I have set up a do loop that I think will work, but does anyone know how to set up those kinds of specifications for the password? Especially the one specifically for the first character?
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Here's for the 1st letter...

Code:
If left(password,1) = Ucase(Left(password,1)) then
.
.
.
End If
 
Upvote 0
Try like this

Code:
If (Asc(Left(pw), 1)) < 65 or Asc(Left(pw, 1)) > 90 Then
'password fails
 
Upvote 0
Checking ascii codes might be more straight forward, but this will, perhaps, expand your coding horizons.

Code:
Sub testpassword()

    pass = "ABCD1234"
    MsgBox (checkValidPassword(pass))

End Sub



Function checkValidPassword(ByVal pwd As String) As Boolean
    If Len(pwd) <> 8 Then Exit Function
    If Not isUpperCaseLetter(Mid(pwd, 1, 1)) Then Exit Function
    
    checkValidPassword = True
    For i = 2 To Len(pwd)
        If Not myIsNumber(Mid(pwd, i, 1)) And Not isUpperCaseLetter(Mid(pwd, i, 1)) Then checkValidPassword = False
    Next i


End Function

Function isUpperCaseLetter(ByVal c As String) As Boolean
    If Len(c) <> 1 Then Exit Function
    If UCase(c) <> LCase(c) Then                            'verify the character is a letter
        If UCase(c) = c Then isUpperCaseLetter = True       'verify that the letter is uppercase
    End If
End Function

Function myIsNumber(ByVal c As String) As Boolean
    Dim i As Integer
    
    If Len(c) <> 1 Then Exit Function
    
    On Error Resume Next
    i = c * 1                                   'this creates an error if c is not a number
    If Err.Number = 0 Then myIsNumber = True    'if no error then true
End Function
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top