gpt4 book ai didi

VBA - 验证我的密码是否包含 1 个数字、1 个 ucase 和 1 个 lcase

转载 作者:行者123 更新时间:2023-12-04 21:22:34 25 4
gpt4 key购买 nike

我有一组密码,我需要验证每个密码是否包含 1 个数字、1 个大写字母、1 个小写字母以及长度是否为 8 个字符。

我制作了这段代码,但它不适用于字符,仅适用于长度。
你能帮我吗?谢谢!!

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

最佳答案

看起来你试图一次做太多事情。稍微拆分代码并没有什么大问题。很多时候,它使阅读和理解正在发生的事情变得更容易。

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

关于VBA - 验证我的密码是否包含 1 个数字、1 个 ucase 和 1 个 lcase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40133375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com