VBA to check username and then allow access to print out

SharmaAntriksh

New Member
Joined
Nov 8, 2017
Messages
31
Hello All, i want to create a VBA code that can help in identifying if the username of the user is anything from (Susan, Alex, Amy, Christina) then only allow them access to print the file. if there is anything you can help me with then it would be great!
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Maybe
Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
   Dim Ary As Variant
   Ary = Array("Susan", "Alex", "Amy", "Christina")
   If Not UBound(Filter(Ary, Environ("Username"), True, vbTextCompare)) >= 0 Then
      Cancel = True
      MsgBox "You are not allowed to print this"
   End If
End Sub
This needs to go in the ThisWorkbook module
 
Upvote 0
Thank you Fluff, can you please confirm why you have used Ubound function here, because i think that only returns the highest value from the array().
 
Upvote 0
If Amy logs in then this
Code:
Filter(Ary, Environ("Username"), True, vbTextCompare)
will filter the array & return Amy. So Ubound(...) returns 0 as there is a value in the filtered array.
Whereas if I logged in There would be nothing in the filtered array so Ubound(...) returns -1
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top