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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
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,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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