Your code is not complete; it is just a fragment. What Sub is it in? What module is it in?
when a user leaves the active sheet
When the user leaves any active sheet? Or is there one sheet in particular you are interested in?
If it's one sheet in particular use this code in the VBA module for that sheet:
Private Sub Worksheet_Deactivate()
' call the Sub that does the sort
End Sub
Also your code looks like it is from the macro recorder, so has some bloat. I would put it in the code module for sheet Project Role and rewrite it like this:
With Me.Sort
.SortFields.Clear
.SortFields.Add Key:=Range("B3"), _
SortOn:=xlSortOnValues, _
Order:=xlAscending, _
DataOption:=xlSortTextAsNumbers
.SetRange Range("B4:B42")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
Bookmarks