2013-09-23 37 views
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") 
+0

添加变量来计算连续的空格数(复位到0时你打到一个非空白的单元格),并在计数大于某个阈值时退出循环 - 例如。在连续10次空白后退出。 –

+0

@tim威廉姆斯..我会尝试..感谢这个想法。 – user2123999

回答

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") 
+0

@ wittrup ..非常感谢! – user2123999

+0

我可以建议添加密码的SHA哈希值吗? http://www.frez.co.uk/vb6.aspx – wittrup

+0

@ wittrup ..我可能不完全了解其中的一些,但作为一个begginer,我很确定我可以从中学习。谢谢! – user2123999