我在关闭之前创建了一些必需的单元格,并希望有一些用户来升级必需的单元格。从here找到这些代码,试图编辑代码,但它不起作用。
希望有人可以向我解释。
u,sers1,u,sers2是不需要填充必需单元格的用户,他们可以关闭工作簿,同时将单元格留空。 excel中的用户名格式为u, ser1
。
感谢任何建议和帮助。
Function IsInvalidUser() As Boolean
Dim asUsers() As String
asUsers = Split("u, ser1.u, ser2", ".")
IsInvalidUser = IsError(Application.Match(Environ("UserName"), asUsers, 0))
End Function
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not (IsInvalidUser) And Cells(2, 8).Value = "" Then
MsgBox "Cell H2 requires user input", vbInformation, "Please filled up the mandatory cells"
Cancel = True
ElseIf Not (IsInvalidUser) And Cells(4, 4).Value = "" Then
MsgBox "Cell D4 requires user input", vbInformation, "Please filled up the mandatory cells"
Cancel = True
End If
End Sub
答案 0 :(得分:2)
似乎阵列难以维护。我会在隐藏的工作表上使用列表。 Scripting.Dictionary也可以很好地工作,并具有能够比较文本的附加优势。
Function IsValidUser() As Boolean
With CreateObject("Scripting.Dictionary")
.CompareMode = vbTextCompare
.Add "u, ser1", vbNullString
.Add "u, ser2", vbNullString
IsValidUser = .Exists(Environ("UserName"))
End With
End Function
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Not IsValidUser Then
If Cells(2, 8).Value = "" Then
MsgBox "Cell H2 requires user input", vbInformation, "Please filled up the mandatory cells"
ElseIf Cells(4, 4).Value = "" Then
MsgBox "Cell D4 requires user input", vbInformation, "Please filled up the mandatory cells"
End If
Cancel = True
End If
End Sub
注意:我更改逻辑以检查有效用户。