2014-09-24 2 views
0

일부 지원 및 지원을 위해 스택 오버플로를 트롤링했으며 아래 코드를 찾았습니다. 이것을 사양으로 변경했습니다. 내가 VBA 코드를 원한다면 로그인을 클릭하면 "액세스 목록"이라는 시트를 검색하고 사용자가 시트에 있는지 확인합니다. 사용자가 발견되면 용지를 표시하십시오 (Pull Print, Push Print, USB, Thresholds 및 Site Contacts). 이 작업을 완료하면 "환영"페이지를 숨기고 싶습니다.Excel VBA 로그인하고 일정 수의 시트를 표시하려면

나는이 부분을 코딩 할 수 있었지만, 내가 고민하는 부분은 사용자가 사용자 이름 & 암호 필드를 공백으로 남겨두고 로그인 만하면된다는 것이다. 제가 보는 메시지가

개체

누군가가 나를 도와 주실 래요이 속성 또는 메서드를 지원하지 않습니다입니까?

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("Access List") 
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 
    Else 
    Sheets("Home").Visible = xlSheetHidden 
    Sheets("Pull Print").Visible = True 
    Sheets("Push Print").Visible = True 
    Sheets("USB").Visible = True 
    Sheets("Thresholds").Visible = True 
    Sheets("Site Contacts").Visible = True 
    Sheets("Access List").Visible = xlSheetHidden 

     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 

답변

0

txtlogin 필드에 일관되게 액세스하지 않습니다. 때로는 Me.을 사용하고 때로는 사용하지 않는 경우도 있습니다. 이처럼 경우 문을 변경

Id = LCase(txtlogin) 

그렇지 않은 경우 : 여기에

Id = LCase(Me.txtlogin) 

:

당신이 줄 하나의 통합 문서 내에서 양식 모듈 교환에 가정 :

If Len(Trim(Me.txtlogin)) = 0 Then 
    ... 
If Len(Trim(Me.txtpassword)) = 0 Then 
    .... 

On Error 행을 제거하여 오류를 발생시키는 행을 찾을 수 있습니다.

+0

나는 이것을 반영하기 위해 코드를 변경했으며 로그인을 클릭해도 여전히 페이지 숨기기가되지 않습니다. –

0

단추와 텍스트 상자가 양식에있는 경우이 오류가 발생하지 않습니다. 그 가정
때문에, 워크 시트에 거주 :

대신 :

txtlogin.SetFocus 
txtpassword.SetFocus 

사용 이들을 :

txtlogin.Activate 
txtpassword.Activate 

추가 된 답 : 그래서, 가정 이전 오류가 후 사라
위의 내용을 사용하면 다음과 같은 완벽한 결과를 얻을 수 있습니다 (찾고자하는 경우 답변으로 표시).

Private Sub CommandButton1_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 txtlogin.Value = "" Then 
txtlogin.Activate 
MsgBox "cant be empty" 
Exit Sub 
End If 

If txtpassword.Value = "" Then 
txtpassword.Activate 
MsgBox "cant be empty" 
Exit Sub 
End If 

Application.ScreenUpdating = False 

Set ws = Worksheets("Access List") 
Id = LCase(Me.txtlogin) 
'MsgBox Id 
pw = Me.txtpassword 

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

'If UserID match found 
If Not aCell Is Nothing Then 
RowNo = aCell.Row 
If pw = aCell.Offset(, 1) Then 
MsgBox "user : " & Id & " logged in" 
txtpassword.Value = "" 
txtlogin.Value = "" 

'do your sheet visibility on off here, like these: 
Sheets("USB").Visible = True 
Sheets("Access List").Visible = xlSheetHidden 
Else 
txtpassword.Value = "" 
MsgBox "Hello " & Id & " ,Password did not match, Please try again", vbOKOnly 
txtpassword.Activate 
End If 

Else 'If no match found 

MsgBox "Unable to match UserID, Please try again", vbOKOnly 
txtlogin.Value = "" 
txtpassword.Value = "" 
txtlogin.Activate 
'MsgBox "Wrong Password, Please try again", vbOKOnly 
End If 
CleanExit: 
Set aCell = Nothing 
Set ws = Nothing 
Application.ScreenUpdating = True 
Exit Sub 
ErrorHandler: 
MsgBox Err.Description 
Resume CleanExit 
End Sub 
+0

지금 구현했습니다.하지만 이제는 내 액세스 목록에 사람들을 추가 할 때 여전히 로그인하지 않습니다. –

관련 문제