这是我拥有的VBA代码,唯一有效的是第一个和最后一个if
和then
语句。但中间的两个没有。我想要发生的是,如果用户名打开工作簿,excel将只显示该人的工作表。该工作簿将基本上为不同的人提供不同的工作表,并将共享。有没有人知道如何修复代码?
Private Sub Workbook_Open()
On Error Resume Next
If VBA.Environ("username") = "Joseph" Then
Worksheets("Joseph").Visible = xlSheetVisible ' First sheet to be made visible
Worksheets("Mark").Visible = xlSheetVeryHidden
Worksheets("Joel").Visible = xlSheetVeryHidden
Worksheets("Ed").Visible = xlSheetVeryHidden
Else
If VBA.Environ("username") = "Mark" Then
Worksheets("Mark").Visible = xlSheetVisible ' First sheet to be made visible
Worksheets("Joseph").Visible = xlSheetVeryHidden
Worksheets("Joel").Visible = xlSheetVeryHidden
Worksheets("Ed").Visible = xlSheetVeryHidden
Else
If VBA.Environ("username") = "Joel" Then
Worksheets("Joel").Visible = xlSheetVisible ' First sheet to be made visible
Worksheets("Ed").Visible = xlSheetVeryHidden
Worksheets("Joseph").Visible = xlSheetVeryHidden
Worksheets("Mark").Visible = xlSheetVeryHidden
Else
If VBA.Environ("username") = "Ed" Then
Worksheets("Ed").Visible = xlSheetVisible ' First sheet to be made visible
Worksheets("Joseph").Visible = xlSheetVeryHidden
Worksheets("Mark").Visible = xlSheetVeryHidden
Worksheets("Joel").Visible = xlSheetVeryHidden
End If
End If
End If
End If
End Sub
答案 0 :(得分:0)
Here是了解Select Case
。
除此之外,elseif应该是一行
Private Sub Workbook_Open()
If VBA.Environ("username") = "Joseph" Then
Worksheets("Joseph").Visible = xlSheetVisible ' First sheet to be made visible
Worksheets("Mark").Visible = xlSheetVeryHidden
Worksheets("Joel").Visible = xlSheetVeryHidden
Worksheets("Ed").Visible = xlSheetVeryHidden
ElseIf VBA.Environ("username") = "Mark" Then
Worksheets("Mark").Visible = xlSheetVisible ' First sheet to be made visible
Worksheets("Joseph").Visible = xlSheetVeryHidden
Worksheets("Joel").Visible = xlSheetVeryHidden
Worksheets("Ed").Visible = xlSheetVeryHidden
ElseIf VBA.Environ("username") = "Joel" Then
Worksheets("Joel").Visible = xlSheetVisible ' First sheet to be made visible
Worksheets("Ed").Visible = xlSheetVeryHidden
Worksheets("Joseph").Visible = xlSheetVeryHidden
Worksheets("Mark").Visible = xlSheetVeryHidden
ElseIf VBA.Environ("username") = "Ed" Then
Worksheets("Ed").Visible = xlSheetVisible ' First sheet to be made visible
Worksheets("Joseph").Visible = xlSheetVeryHidden
Worksheets("Mark").Visible = xlSheetVeryHidden
Worksheets("Joel").Visible = xlSheetVeryHidden
End If
End Sub
然后按顺序进行,直到找到一个为真,然后跳过所有其余部分。逻辑与excel中的If
函数略有不同。你可以在一个语句中有多个elseif。
答案 1 :(得分:0)
米
提供此代码并尝试告诉我。您不必编写所有案例代码,因为如果您向公司添加新用户会发生什么。看看一个更好的方法,你只需要更新和数组
Option Explicit
' Module Variable to hold the users.
Dim arrUsers As Variant
Private Sub Workbook_Open()
Dim strUserName As String
' Load the arrau of users and the username
arrUsers = Array("Joseph", "Mark", "Joel", "Ed")
strUserName = Environ$("username")
' Unhide the correct sheet.
Call MakeVisbleForUser(strUserName)
On Error Resume Next
End Sub
' Hide all the sheets and show only the one for an user.
Sub MakeVisbleForUser(ByVal strUser As String)
Dim lCountVisible As Long
Dim i As Long
lCountVisible = GetCountVisibleSheets
' Hide all the sheets
For i = LBound(arrUsers) To UBound(arrUsers)
' Make sure that the sheet for the user is not hidden
If Not arrUsers(i) = strUser Then
' Make sure that there is at least one sheet visible
If Not lCountVisible = 1 Then
Worksheets(arrUsers(i)).Visible = xlSheetVeryHidden
lCountVisible = lCountVisible - 1
End If
End If
Next i
' Show only the one for the user
Worksheets(strUser).Visible = xlSheetVisible
End Sub
Function GetCountVisibleSheets() As Long
Dim sh As Worksheet
Dim ret As Long
For Each sh In ThisWorkbook.Sheets
If sh.Visible = xlSheetVisible Then
ret = ret + 1
End If
Next sh
' Return the value
GetCountVisibleSheets = ret
End Function
谢谢,我希望它有所帮助:)
答案 2 :(得分:0)
Here is a simpler way:
Worksheets("Joseph").Visible = xlSheetVeryHidden
Worksheets("Mark").Visible = xlSheetVeryHidden
Worksheets("Joel").Visible = xlSheetVeryHidden
Worksheets("Ed").Visible = xlSheetVeryHidden
Worksheets(VBA.Environ("username")).Visible = xlSheetVisible