我知道这是一个简单的问题,但我很难用这个,我有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")
答案 0 :(得分: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")