0

我知道这是一个简单的问题,但我很难解决这个问题,我有 2 个文本框(txtUs,txtPass)和一个命令/注销按钮,用于验证数据并让他们输入用户名和密码。如果他们输入了密码或用户名错误,会出现错误信息。但是如果他们得到了两个正确的信息,它将显示一条消息“您已成功注销”。问题是,如果前面有空行,程序就找不到数据。如果数据之前没有从 A2 列开始的空白列,它只会识别数据。任何帮助/更正将不胜感激。

Dim user_name As String
Dim user_pass As String

If Not IsNull(UserForm4.txtUs) Then
    user_name = UserForm4.txtUs

Else
    MsgBox "Username or password is Incorrect"
    Exit Sub
End If

If Not IsNull(UserForm4.txtPuss) Then
    user_pass = UserForm4.txtPuss
Else
    MsgBox "Username or password is Incorrect"
    Exit Sub
End If

Dim counter As Integer
counter = 2
Do Until ThisWorkbook.Sheets("Sheet2").Cells(counter, 1).Value = ""
    If ThisWorkbook.Sheets("Sheet2").Cells(counter, 1).Value = user_name And ThisWorkbook.Sheets("Sheet2").Cells(counter, 2).Value = user_pass Then
        MsgBox ("You have been logged-out")
        UserForm5.txt_Mon_in.Text = Format(ThisWorkbook.Sheets("Sheet2").Cells(counter, 4).Value, "hh:mm AMPM")
        UserForm5.txt_Mon_out.Text = Format(Time, "hh:mm AMPM")
        UserForm5.Label1 = Sheets("employees").Cells(counter, 2).Value & Sheets("employees").Cells(counter, 3).Value & Sheets("employees").Cells(counter, 4).Value
        UserForm5.Label2 = Date + 14
        UserForm5.txt_Mon_Rate.Text = Sheets("employees").Cells(counter, 6).Value
        UserForm5.Show
        ThisWorkbook.Sheets("Sheet2").Cells(counter, 5).Value = Time
        UserForm4.Hide
        Set UserForm4 = Nothing
        Exit Sub
    End If
    counter = counter + 1
Loop
MsgBox ("Username or password is incorrect")
4

1 回答 1

0

通过 usedrange 更改为 for 循环

Sub test()
Dim c As Range
For Each c In ThisWorkbook.Sheets("Sheet2").UsedRange
        counter = c.Row
    Next
End Sub

添加检查以跳过空单元格:

Dim user_name As String
Dim user_pass As String

If Not IsNull(UserForm4.txtUs) Then
user_name = UserForm4.txtUs

Else
MsgBox "Username or password is Incorrect"
Exit Sub
End If

If Not IsNull(UserForm4.txtPuss) Then
user_pass = UserForm4.txtPuss
Else
MsgBox "Username or password is Incorrect"
Exit Sub
End If

Dim counter As Integer

Dim c As Range
For Each c In ThisWorkbook.Sheets("Sheet2").UsedRange
counter = c.Row

If c.Value <> "" then
If ThisWorkbook.Sheets("Sheet2").Cells(counter, 1).Value = user_name And ThisWorkbook.Sheets("Sheet2").Cells(counter, 2).Value = user_pass Then
MsgBox ("You have been logged-out")
UserForm5.txt_Mon_in.Text = Format(ThisWorkbook.Sheets("Sheet2").Cells(counter, 4).Value, "hh:mm AMPM")
UserForm5.txt_Mon_out.Text = Format(Time, "hh:mm AMPM")
UserForm5.Label1 = Sheets("employees").Cells(counter, 2).Value & Sheets("employees").Cells(counter, 3).Value & Sheets("employees").Cells(counter, 4).Value
UserForm5.Label2 = Date + 14
UserForm5.txt_Mon_Rate.Text = Sheets("employees").Cells(counter, 6).Value
UserForm5.Show
ThisWorkbook.Sheets("Sheet2").Cells(counter, 5).Value = Time
UserForm4.Hide
Set UserForm4 = Nothing
Exit Sub
End If
counter = counter + 1
Next
MsgBox ("Username or password is incorrect")
于 2013-09-23T18:24:19.820 回答