我正在尝试创建一个登录页面,当用户不是管理员时,工作表(列表、引擎和管理)将隐藏,否则可见。下面的代码仅隐藏最后一个工作表,所有其他工作表对用户都可见。
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
我尝试了上述方法并尝试使用循环,但由于某种原因似乎无法让它工作。
如果我理解正确,那么您不需要循环来显示/隐藏工作表。这应该有帮助。
Dim VisibleStatus As Integer
If Sheets("Admin").Range("B8").Value = "Yes" Then _
VisibleStatus = xlSheetVisible Else VisibleStatus = xlSheetVeryHidden
Sheets("Admin").Visible = VisibleStatus
Sheets("Engine").Visible = VisibleStatus
Sheets("Lists").Visible = VisibleStatus
indentation
格式化代码是一种很好的编码实践,因为它有助于更轻松地识别和修复错误。Next
和 End If
以保持正确的结构。Admin
工作表。If Wksht.Name = "Engine" Then
语句中nested if
内的条件始终计算为False
。' OP's code in indentation w/o changes
Private Sub okbtn_Cldick()
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
InStr
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
Else
Wksht.Visible = xlSheetVisible
End If
Next
Sheets("Admin").Range("B4,B5").ClearContents
Sheets("Phonebook").Activate
Else
MsgBox "Please enter a correct username and password"
End If
End Sub
微软文档:
Private Sub okbtn_Click()
With ThisWorkbook.Sheets("Admin")
If .Range("B6").Value = True Then ' username and password in 'B4,B5'?
Application.ScreenUpdating = False
.Range("B7").Value = .Range("B4").Value 'set current user
'login_UF.Hide
' If user is 'Admin', in 'B8' is 'Yes'?
Dim Visibility As XlSheetVisibility: Visibility = IIf( _
.Range("B8").Value = "Yes", xlSheetVisible, xlSheetVeryHidden)
Dim ws As Worksheet, SheetName As Variant
' Assuming the non-admin sheets are visible either way:
For Each SheetName In Array("Admin", "Engine", "Lists")
Set ws = .Parent.Sheets(SheetName)
If ws.Visible <> Visibility Then ' incorrect visibility
ws.Visible = Visibility
'Else ' correct visibility; do nothing
End If
Next SheetName
.Range("B4,B5").ClearContents ' clear username and password?
.Parent.Sheets("Phonebook").Activate
Application.ScreenUpdating = True
Else ' no username and/or password in 'B4,B5'?
MsgBox "Please enter your username and password!", vbExclamation
End If
End With
End Sub
我能够使用taller的示例对其进行一些修改,使其正常工作。
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 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
Else: ' not admin
Wksht.Visible = xlSheetVisible
End If
Next
Sheets("Admin").Range("B4,B5").ClearContents
Sheets("Phonebook").Activate
Else
MsgBox "Please enter a correct username and password"
End If
End Sub