The logic on my code depends on the user who is running the macro.
e.g. if username is “abc” or “def” do something and if not do something other.
That why I use the same lengthy code of If conditions many times.
Is there any way to save IF condition as Boolean Variable ?
Appreciate for any useful comments and answers.
Option Explicit
Option Compare Text
Sub Run_Depends_on_Username()
If UserName = "ABC" Or UserName = "DEF" Or UserName = "XYZ" Then
'Code
End If
End Sub
And this is the function to get username
Function UserName() As String
UserName = Environ("username")
End Function
>Solution :
Generally, you can assign the result of a boolean expression directly to a variable, eg
Dim isAllowed as boolean
isAllowed = (UserName = "ABC" Or UserName = "DEF" Or UserName = "XYZ" )
' Use it:
if isAllowed Then
Alternative is to create a simple function
Function isAllowed() As Boolean
isAllowed = (UserName = "ABC" Or UserName = "DEF" Or UserName = "XYZ")
End Function