2012-03-15 59 views
3
Private Sub cmdLogin_Click() 
On Error GoTo ErrorHandler 

Dim RowNo As Long 
Dim Id As String 
Dim pw As String 
Dim ws As Worksheets 

Application.ScreenUpdating = False 
Set ws = Worksheets("User&Pass") 
Id = LCase(Me.txtLogin) 


RowNo = Application.WorksheetFunction.Match(Id, ws.range("A2:A999"), 0) 

CleanExit: 
Set ws = Nothing ' free memory 
Application.ScreenUpdating = True ' turn on the screen updating 
Exit Sub 

ErrorHandler: 
MsgBox "Unable to match ID, enter valid ID.", vbOKOnly 
GoTo CleanExit 

End Sub 

我有一個excel用戶表單我一直在努力,現在我需要它通過登錄屏幕看起來更專業。我已經開始使用上面的代碼,但是我已經陷入了死衚衕。Excel vba用戶名/密碼查找

如何設置我的目的是說如果密碼匹配,然後加載工作簿或取消隱藏工作簿並繼續。用戶名和密碼位於名爲「用戶&通行證」的表單中 目的是從a-user/b-pw的列分別讀取,如果它是成功的,我將隱藏該表以便他們不能看到其他用戶的信息

與我上面開始我只需要它說,如果它匹配usercolumn那麼相應的PW隔壁它繼續別人去我的ErrorHandler

我可以做格式化有關隱藏和取消隱藏表等只需要閱讀幫助用戶名和密碼

非常感謝 Z

編輯嘗試一個;

Private Sub cmdLogin_Click() 
On Error GoTo ErrorHandler 
Dim RowNo As Long 
Dim Id As String 
Dim pw As String 
Dim ws As Worksheets 
Application.ScreenUpdating = False 
Set ws = Worksheets("User&Pass") 

Id = LCase(Me.txtLogin) 
RowNo = Application.WorksheetFunction.Match(Id, ws.range("A2:A999"), 0) 
RowNo = RowNo + 1 
pw = ws.range("B" & RowNo) 
If pw = Me.txtLogin Then 
'continue 
txt1.Value = "yes" 
Else 
GoTo ErrorHandler 
End If 


CleanExit: 
Set ws = Nothing ' free memory 
Application.ScreenUpdating = True ' turn on the screen updating 
Exit Sub 
ErrorHandler: 
MsgBox "Unable to match ID, enter valid ID.", vbOKOnly 
GoTo CleanExit 
End Sub 

@siddarthRout

Private Sub cmdLogin_Click() 
Dim RowNo As Long 
Dim Id As String, pw As String 
Dim ws As Worksheet 
Dim aCell As range 
On Error GoTo ErrorHandler 
Application.ScreenUpdating = True 

Set ws = Worksheets("Details") 
Id = LCase(Me.txtLogin) 

Set aCell = ws.Columns(1).Find(What:=Id, LookIn:=xlValues, _ 
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
MatchCase:=False, SearchFormat:=False) 

'~~> If match found 
If Not aCell Is Nothing Then 
RowNo = aCell.Row 
'~~> Rest of your code. For example if the password is 
'~~> Stored in Col B then 
Debug.Print aCell.Offset(, 1) 
Unload Me 
FrmMenu.Show 
'~~> You can then use the above aCell.Offset(, 1) to 
'~~> match the password which the user entered 
Else '<~~ If not found 
MsgBox "Unable to match ID, enter valid ID.", vbOKOnly 
End If 
CleanExit: 
Set ws = Nothing 
Application.ScreenUpdating = True 
Exit Sub 
ErrorHandler: 
MsgBox Err.Description 
Resume CleanExit 
End Sub 
+0

快速提示:這是否意味着是一個非常安全的密碼?特別是以這種方式完成的Excel對此非常不利。 – Gaffi 2012-03-15 13:57:11

+0

如果有一種方法使它看起來熟悉用戶名和密碼,並確保以不同的方式做到這一點我是所有人的耳朵,我的代碼是我嘗試過的一個例子,只是嘗試調整你放入的東西,但沒有運氣 – Zenaphor 2012-03-15 13:59:01

+1

爲什麼不只是有一個授權用戶的列表,然後驗證登錄的Windows用戶名對此?這樣就不需要用戶名和密碼 – SWa 2012-03-15 14:01:02

回答

3

檢測過,並試圖

這是你想什麼呢?

CODE

Option Explicit 

Private Sub cmdLogin_Click() 
    Dim RowNo As Long 
    Dim Id As String, pw As String 
    Dim ws As Worksheet 
    Dim aCell As Range 

    On Error GoTo ErrorHandler 

    If Len(Trim(txtLogin)) = 0 Then 
     txtLogin.SetFocus 
     MsgBox "Username cannot be empty" 
     Exit Sub 
    End If 

    If Len(Trim(txtPassword)) = 0 Then 
     txtPassword.SetFocus 
     MsgBox "Password cannot be empty" 
     Exit Sub 
    End If 

    Application.ScreenUpdating = False 

    Set ws = Worksheets("User&Pass") 
    Id = LCase(Me.txtLogin) 

    Set aCell = ws.Columns(1).Find(What:=Id, LookIn:=xlValues, _ 
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
    MatchCase:=False, SearchFormat:=False) 

    '~~> If match found 
    If Not aCell Is Nothing Then 
     RowNo = aCell.Row 
     If Me.txtPassword = aCell.Offset(, 1) Then 
      FrmMenu.Show 
      Unload Me 
     Else 
      MsgBox "Unable to match UserID or PasswordID, Please try again", vbOKOnly 
     End If 
    Else '<~~ If not found 
     MsgBox "Unable to match UserID or PasswordID, Please try again", vbOKOnly 
    End If 
CleanExit: 
    Set ws = Nothing 
    Application.ScreenUpdating = True 
    Exit Sub 
ErrorHandler: 
    MsgBox Err.Description 
    Resume CleanExit 
End Sub 

提示

不要讓什麼是不正確的用戶知道(從安全的角度來看) - 用戶名或密碼。始終顯示的通用消息像「無法匹配用戶名或PasswordID,請重試」 :)

HTH

希德

+0

,提示我錯誤處理程序,而不是讓我卸載進度,以測試它是否進展,我告訴它寫入文本框的值爲「是」,但它只是發送給我的錯誤處理程序 – Zenaphor 2012-03-15 14:41:57

+0

@ Zeaphor:請更新您的第一篇文章,確切的代碼,你正在使用:) – 2012-03-15 14:44:11

+0

我用你上面提到的,離開它的acell偏移量,因爲它是任何旁邊它 – Zenaphor 2012-03-15 15:03:07

1

另一種方式

On Error Resume Next 
If Me.password <> Application.VLookup(Me.username, Sheet1.Cells(1, 1).CurrentRegion, 2, False) Then 
    MsgBox ("incorrect") 
    Exit Sub 
Else 
    MsgBox ("Correct Password Entered") 
End If 

你也將需要確保所有工作表從一開始就是xlSheetVeryHidden,以防止宏被禁用,並將其作爲成功登錄例程的一部分取消隱藏。您還需要在VBA項目上設置密碼,以防止人員取消隱藏表單。但請記住,Excel和溼紙袋一樣安全;)

+0

是的,我認爲以上所有,人們不會試圖闖入它,因爲它是一個公司的工具,很好將是,只有這樣管理者纔會認爲更有控制力 – Zenaphor 2012-03-16 07:33:19