Restrict Access per user

Chiragshah15

New Member
Joined
May 16, 2012
Messages
18
Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
I wanted to build some VBA code to restrict what a user can see based on their user ID. <o:p></o:p>
<o:p></o:p>
I am deciding on whether to use a pivot table with slicers to create the restrictions or drop down boxes.<o:p></o:p>
<o:p></o:p>
Can anyone suggest any useful code or sites?<o:p></o:p>
<o:p></o:p>
Thanks,<o:p></o:p>
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
As a workbook_open event, use environ("username") to specify what details a user can access
 
Upvote 0
Try code below. Does it help?

Code:
Sub Test()
'_________________________________________
'   Test UserName authorisation
    Dim objNet As Object
    Dim Msg As String
    'Dim C As Range, aRange As Range, aCell As Range
 
    On Error Resume Next
    Set objNet = CreateObject("WScript.NetWork")
      Select Case objNet.UserName
        Case Is = "bSmith", "jsmith", "super" '<==== UserName Access
 
  'Access granted add your code and action
 
 
        Case Else
          MsgBox "Access Is Denied " & objNet.UserName
      End Select
 
End Sub
 
Upvote 0
Great thank you,

works well, you know that as you have given me some help I will need to ask you for more?

I have a list of people that I wanted to give access to. There access is determined by a filter on cell a1 (basically thier user name)

e.g

If Chris Dawes (Dawesc) opens the excel spreadsheet the filter will only select his name.

So far I have got the code below, but I see it more unmanageable if I have many users, would it be possible to lookup the user on a lst in the workbook?

On Error Resume Next
Set objNet = CreateObject("WScript.NetWork")
Select Case objNet.UserName
Case Is = "bSmith", "jsmith", "shahca" '<==== UserName Access

'Access granted add your code and action
Sheets("pivot").Visible = True
ActiveSheet.Range("$A$1:$at$5").AutoFilter Field:=1, Criteria1:=objNet.UserName
Cells.Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Paste
ActiveSheet.Name = objNet.UserName
Sheets("pivot").Visible = False

MsgBox "Report created for " & objNet.UserName

Case Else
MsgBox "Access Is Denied " & objNet.UserName
End Select

End Sub
 
Upvote 0
Try code below

AccessList is ranged name where you define userid you want access granted for.

Code:
Sub test()
'Test UserName authorisation
    Dim objNet As Object
    Dim Msg As String
    Dim iBoolean As Boolean
    Dim i As Long
   
   
On Error Resume Next
Set objNet = CreateObject("WScript.NetWork")
iBoolean = False
 
iBoolean = Application.IsNumber(Application.Match(CStr(objNet.UserName), Range("AccessList"), 0))
'MsgBox iBoolean, vbInformation
Select Case iBoolean = True
Case True:
'Access granted add your code and action
Sheets("pivot").Visible = True
ActiveSheet.Range("$A$1:$at$5").AutoFilter Field:=1, Criteria1:=objNet.UserName
Cells.Select
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Paste
ActiveSheet.Name = objNet.UserName
Sheets("pivot").Visible = False
MsgBox "Report created for " & objNet.UserName
Case False:
MsgBox "Access Is Denied " & objNet.UserName
End Select
End Sub

Does it help?

Biz
 
Upvote 0
Welcome to the Board!

If you PM me your e-mail address I'll send you a workbook that does what you want. A few of us put it together several years ago for another board member, and it's been floating around ever since.
 
Upvote 0

Forum statistics

Threads
1,215,430
Messages
6,124,853
Members
449,194
Latest member
HellScout

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