title: VBA - how to specify valid users to perform certain action
body: <p>I had created some mandatory cells beforeclose, and would like to have some user to escae the mandatory cells. Found these code from <a href="https://answers.microsoft.com/en-us/msoffice/forum/msoffice_excel-mso_winother/excel-vba-only-run-macro-if-your-windows-user-name/d8f29b1f-82de-4f7f-8730-c8cca66e1206?auth=1" rel="nofollow noreferrer">here</a>, trying to edit the codes but it does not work. </p>
  
  <p>Hope someone could explain it to me.
  The u, sers1, u, sers2 are the users that no need to fill up the mandatory cells, they can close the workbook while leaving the cells blank. The username format in the excel is <code>u, ser1</code>.</p>
  
  <p>Appreciate any suggestions and help.</p>
  
  <pre><code>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
  </code></pre>