Macro to select name in slicer

AndrewD04

New Member
Joined
Aug 24, 2017
Messages
40
Office Version
  1. 365
Morning, Is it possible to use a macro to detect your username and then select that username in a slicer
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
For the username associated with the operating system, you can use the Environ function...

Code:
environ("username")

For the username associated with the application, you can use the UserName property of the Application object...

Code:
application.UserName

Hope this helps!
 
Upvote 0
For the username associated with the operating system, you can use the Environ function...

Code:
environ("username")

For the username associated with the application, you can use the UserName property of the Application object...

Code:
application.UserName

Hope this helps!

Thank you so much, do you know a way to use that username to select the person in a slicer so it displays only their data
 
Upvote 0
Try the following...

Code:
[FONT=Courier New][COLOR=darkblue]Sub[/COLOR] SelectUserFromSlicer()

    [COLOR=darkblue]Dim[/COLOR] oSlicerItem         [COLOR=darkblue]As[/COLOR] SlicerItem
    [COLOR=darkblue]Dim[/COLOR] sUserName           [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    
    Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
    
    sUserName = Environ("username") [COLOR=green]'or Application.UserName[/COLOR]
    
    [COLOR=darkblue]With[/COLOR] ThisWorkbook.SlicerCaches("Slicer_User") [COLOR=green]'change the slicer name accordingly[/COLOR]
        .ClearManualFilter
        [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
        [COLOR=darkblue]Set[/COLOR] oSlicerItem = .SlicerItems(sUserName)
        [COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] 0
        [COLOR=darkblue]If[/COLOR] [COLOR=darkblue]Not[/COLOR] oSlicerItem [COLOR=darkblue]Is[/COLOR] [COLOR=darkblue]Nothing[/COLOR] [COLOR=darkblue]Then[/COLOR]
            [COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] oSlicerItem [COLOR=darkblue]In[/COLOR] .SlicerItems
                [COLOR=darkblue]If[/COLOR] UCase(oSlicerItem.Name) <> UCase(sUserName) [COLOR=darkblue]Then[/COLOR]
                    oSlicerItem.Selected = [COLOR=darkblue]False[/COLOR]
                [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
            [COLOR=darkblue]Next[/COLOR] oSlicerItem
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
    
    Application.ScreenUpdating = [COLOR=darkblue]True[/COLOR]
    
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR][/FONT]

Hope this helps!
 
Upvote 0
Hi,
I know, this is quite old topic, but I need exactly the same: when user opens excel, the slicer filters the data according to his windows username.
I tried the code from previous post but all I get is cleared slicer, but I don't get selection (username) in slicer.

Can somebody help?
tnx
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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