2013-09-23 36 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