首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >excel VBA for VBA(登录系统)

excel VBA for VBA(登录系统)
EN

Stack Overflow用户
提问于 2022-02-09 14:19:37
回答 1查看 38关注 0票数 0

嗨,我只是好奇为什么在试图运行代码时会出现这个错误。下面的代码是一个登录系统。发生的错误是“没有下一步”,我已经多次尝试重构代码,但它仍然不起作用。我在VBA方面没有太多经验,所以任何帮助都是非常感谢的。

代码语言:javascript
复制
Sub CommandButton1_Click()
Dim Username As String
Username = Range("D6").Value
UsernameLength = Len(Username)
MsgBox (UsernameLength)

Dim PasswordLength As String
Password = Range("D10").Value
PasswordLength = Len(Password)
MsgBox ("Password length is:" + PasswordLength)

If PasswordLength = 0 And UsernameLength = 0 Then
  MsgBox ("Please enter login details or create an account")
Else
Set rng = Range("A1:A1000")
For Each row In rng.Rows
For Each cell In row.Cells
            Username_Lines = Username_Lines + 1
            Password_Lines = Password_Lines + 1
            If cell = Username Then
                Correct_Username = True
                a = Username_Lines
                Lines = a - b
                MsgBox ("True")
                Stop
            Else
               Correct_Password = False
               Correct_Username = False
               MsgBox ("False")
               End If
               Next
If Lines = 0 Then
    Correct_Password = True
    Correct_Username = True
Else:
    Correct_Password = False
    Correct_Username = False
    End If
               
If Correct_Password And Correct_Username = True Then
        Worksheets("MainSystem").Visible = xlSheetVisible
        Worksheets("LoginSystem").Visible = Not xlSheetVisible

ElseIf Correct_Password = Sql Or Correct_Username = Sql Then
        MsgBox ("Login Failed")

ElseIf Correct_Password And Correct_Username = False Then
        MsgBox ("Login Failed")
        
Else:
    MsgBox ("Login Failed")
          
End If
End Sub
EN

回答 1

Stack Overflow用户

发布于 2022-02-09 16:38:12

考虑使用Match替代For每一个循环。

代码语言:javascript
复制
Option Explicit

Private Sub CommandButton1_Click()
    Dim username As String, password As String
    Dim ws As Worksheet, lastrow As Long, rng As Range, r
    
    Set ws = ActiveSheet '  better as Sheets("Sheetname")
    With ws
        username = .Range("D6").Value
        password = .Range("D10").Value
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set rng = .Range("A1:A" & lastrow)
    End With
    
    If Len(username) = 0 Or Len(password) = 0 Then
        MsgBox "Please enter login details or create an account", vbCritical
        Exit Sub
    End If
    
    ' find username
    r = Application.Match(username, rng, 0)
    If IsError(r) Then
         MsgBox ("Login Failed")
    Else
        If ws.Cells(r, "B") = password Then
            MsgBox ("Login OK")
            Sheets("MainSystem").Visible = xlSheetVisible
            Sheets("LoginSystem").Visible = xlSheetHidden
        Else
            MsgBox ("Login Failed")
        End If
    End If
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71051220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档