需要帮助在登录时隐藏工作表

问题描述 投票:0回答:1

我正在尝试创建一个登录页面,当用户不是管理员时,工作表(列表、引擎和管理)将隐藏,否则可见。下面的代码仅隐藏最后一个工作表,所有其他工作表对用户都可见。

Private Sub okbtn_Click()
Dim Wksht As Worksheet
If Sheets("Admin").Range("B6").Value = True Then
Sheets("Admin").Range("B7").Value = Sheets("Admin").Range("B4").Value 'set current user

        login_UF.Hide
        
For Each Wksht In ThisWorkbook.Worksheets

    If Wksht.Name = "Admin" Then
            If Sheets("Admin").Range("B8").Value = "Yes" Then ' admin
                Wksht.Visible = xlSheetVisible
            Else
                Wksht.Visible = xlSheetVeryHidden
             End If
 If Wksht.Name = "Engine" Then
            If Sheets("Admin").Range("B8").Value = "Yes" Then ' admin
                Wksht.Visible = xlSheetVisible
            Else
                Wksht.Visible = xlSheetVeryHidden
             End If
 If Wksht.Name = "Lists" Then
            If Sheets("Admin").Range("B8").Value = "Yes" Then ' admin
                Wksht.Visible = xlSheetVisible
            Else
                Wksht.Visible = xlSheetVeryHidden
             End If

             
         Else: ' not admin
         Wksht.Visible = xlSheetVisible
        End If
        
      
    Next Wksht
    
    
    Sheets("Admin").Range("B4,B5").ClearContents
    Sheets("Phonebook").Activate
    

Else
MsgBox "Please enter a correct username and password"
End If

End Sub

尝试了上述方法并尝试使用循环,但由于某种原因似乎无法让它工作

excel vba database userform
1个回答
0
投票
  • 嵌套如果得到不正确的结果。
Option Explicit
Private Sub okbtn_Click()
    Dim Wksht As Worksheet
    If Sheets("Admin").Range("B6").Value Then
        Sheets("Admin").Range("B7").Value = Sheets("Admin").Range("B4").Value 'set current user
        login_UF.Hide
        For Each Wksht In ThisWorkbook.Worksheets
            If InStr(1, "Admin|Engine|Lists", Wksht.Name, vbTextCompare) > 0 Then
                If Sheets("Admin").Range("B8").Value = "Yes" Then ' admin
                    Wksht.Visible = xlSheetVisible
                Else
                    Wksht.Visible = xlSheetVeryHidden
                End If
                Sheets("Admin").Range("B4,B5").ClearContents
                Sheets("Phonebook").Activate
            Else
                MsgBox "Please enter a correct username and password"
            End If
        Next
    End If
End Sub

© www.soinside.com 2019 - 2024. All rights reserved.