I have an excel macro that needs to know whether it has admin privileges. The macro is running a shell command that will fail if the user didn't choose to run excel as an admin.
-
7The correct way to do this is to call the API function `CheckTokenMembership`. (It used to be simpler, you just called `IsUserAnAdmin`, but that convenience function is deprecated and no longer supported. You have to do it the long way.) Anyway, I have to ask, what are you going to do about it if they don't have administrative privileges? It might make more sense to just handle the error. – Cody Gray - on strike Aug 29 '16 at 17:45
-
@CodyGray Thanks. I'll just ask the user to run the macro as an admin. – TZubiri Aug 29 '16 at 17:46
3 Answers
There is a microsoft function used to determine whether the user running your application has admin privileges.
First include the library this function resides in.
Private Declare Function IsUserAnAdmin Lib "shell32" () As Long
Then just call the function and utilize the return boolean in any way
if IsUserAnAdmin() then
//Ask user to run application as admin
End if

- 886
- 11
- 30
Here in W11 x64, MSO Excel 2021 x32 (v2301 b16.0.16026.20002) the below code run:
Option Explicit
'EXCEL x64.
#If VBA7 Then
Public Declare PtrSafe Function IsUserAnAdmin Lib "shell32" Alias "#680" () As Boolean
'EXCEL x32.
#Else
Public Declare Function IsUserAnAdmin Lib "shell32" Alias "#680" () As Boolean
To test:
Sub Test()
If IsUserAnAdmin Then MsgBox ("User got Admin's right")
End Sub

- 120
- 1
- 2
- 10
The accepted answer no longer works in W10. I did find a few days ago some complicated code but can't find it again.
All I can find is at [https://www.elevenforum.com/t/check-account-is-administrator-or-standard-user-in-windows-11.9556/]
where it advocates using vbs or batch file:
net localgroup administrators
and analysing the output.
After discovering that the above did not work on a server that was part of a domain (and presumably same for a pc) I have found that the following does work for both types:
gpresult /user xxxxxx /r > yyyyyy
where xxxxxx is the user's name and yyyyyy is the file name for the redirected output.
(/user xxxxxx may be omitted as the default is the currently logged in user)
You need to read back the file looking for a line that contains 'Administrators'.
If found then the user is a member of that group.

- 35
- 6
-
1.. what about upgrading [CheckTokenMembership](https://stackoverflow.com/questions/1865508/checktokenmembership-in-vb6) to current requirements in VBA7/64bit? – T.M. Dec 29 '22 at 08:12
-
Testing this idea of using NET - it works fine on a stand-alone PC. Tried it on a server that is part of a domain controller network and it does not work - it does not list users. – LesD Dec 29 '22 at 11:18
-
-
-
1Cody Gray♦ above [commented](https://stackoverflow.com/questions/39212104/how-to-check-admin-privileges-in-vba/74947808#comment65763069_39212104) about `IsUserAnAdmin` not working in 2016. The [question](https://stackoverflow.com/q/1865508/11683) that you posted a [non-answer](https://meta.stackoverflow.com/q/253963/11683) under (which will likely be deleted) contains a proper solution with `CheckTokenMembership`, and has a note about `IsUserAnAdmin` being deprecated [dated 2009](https://stackoverflow.com/questions/1865508/checktokenmembership-in-vb6#comment1763630_1865797). – GSerg Dec 29 '22 at 15:08
-
I tried that solution in MS Access (O365) and it crashes Access every time. It may have worked in 2009 but not now. – LesD Dec 29 '22 at 16:31
-
1Did you correctly redeclare the functions as [suggested above](https://stackoverflow.com/questions/39212104/how-to-check-admin-privileges-in-vba/74947808#comment132261557_74947808), or did you [just add `PtrSafe`](https://stackoverflow.com/a/63801528/11683)? – GSerg Dec 29 '22 at 18:14
-
Sorry - I do not understand your question 'suggested above'. I declared the function, but added PtrSafe - as is necessary for 64 bit. My test does not generate an error, it just returns neither true or false. As reported elsewhere, the IsUserAnAdmin fn no longer works in W10 (nor in 8.1 I believe). – LesD Dec 30 '22 at 03:46
-
1So you [simply added `PtrSafe`](https://stackoverflow.com/a/63801528/11683), instead of converting the `Long`s to `LongPtr`s where they must be. – GSerg Dec 30 '22 at 18:05