multiple conditions for if then else

  • Thread starter Thread starter joemeshuggah
  • Start date Start date
J

joemeshuggah

using the following code to avoid printing restrictions on a specific set of
user ids...what is the correct way to add additional user ids (e.g. User2,
User3, etc) to the if statement?

Private Sub Workbook_BeforePrint(Cancel As Boolean)


If Application.UserName <> "User1" Then

Dim ws As Worksheet
Cancel = True
Application.EnableEvents = False
For Each ws In ActiveWindow.SelectedSheets
Select Case ws.Name
Case "Daily", "Team", "Individual": ws.PrintOut
End Select
Next ws
Application.EnableEvents = True

Else

End If

End Sub
 
see if something like this would work:

Sub test()
Dim ws As Worksheet
Select Case UCase(Application.UserName)
Case "USER1", "USER2"
Application.EnableEvents = False
For Each ws In Worksheets(Array("Daily", "Team", "Individual"))
ws.PrintPreview
Next ws
Application.EnableEvents = True
Case Else
End Select
End Sub
 
Use the Select Case statement as an alternative to using ElseIf in
If...Then...Else statements when comparing one expression to several
different values. While If...Then...Else statements can evaluate a different
expression for each ElseIf statement, the Select Case statement evaluates an
expression only once, at the top of the control structure.

Example1:
If Application.UserName = "User1" Then
your code
Else if
Application.UserName = "User2" then

End if

Example2:
Select Case UserID
Case User1
Your code
Case User2,User3
Your code
Case User4
Your code
Case Else
Your Code
End Select
 
Back
Top