LocationInTable
A property exposed by Range object which returns a constant, derived from XlLocationInTable Enum, helps determining current location in Pivot Table. In this article we will create one pivot table and try to identify selection within using VBA code user defined function. XlLocationInTable enum has following constants:
Constants
- xlRowHeader: Selection is in Row Header
- xlColumnHeader: Selection is in Column Header
- xlPageHeader: Selection is in Pivot Page Header
- xlDataHeader: Selection is in Data Header of the Pivot
- xlRowItem: Selection is in Row Item in the Pivot table
- xlColumnItem: Selection is in Column Item in the Pivot table
- xlPageItem: Selection is in Page Item of Pivot table
- xlDataItem: Selection is in Data Item of Pivot table
- xlTableBody: Selection is in Table Body of Pivot
Pivot Table

Refer above Pivot table on which we will trace selection using VBA code given below:
Code example
Public Function WhatIsMyLocation(myRange As Range) As String 'Declare range object Dim oRange As Range 'Bind selection to Range Set oRange = myRange 'Put case to identify selection Select Case oRange.LocationInTable Case Is = xlRowHeader WhatIsMyLocation = "You are in Header Row" Case Is = xlColumnHeader WhatIsMyLocation = "You are in Column Header" Case Is = xlPageHeader WhatIsMyLocation = "You are in Page Header" Case Is = xlDataHeader WhatIsMyLocation = "You are in Data Header" Case Is = xlRowItem WhatIsMyLocation = "You are in Row Item" Case Is = xlColumnItem WhatIsMyLocation = "You are in Column Item" Case Is = xlPageItem WhatIsMyLocation = "You are in Page Item" Case Is = xlDataItem WhatIsMyLocation = "You are in Data Item" Case Is = xlTableBody WhatIsMyLocation = "You are in Table Body" Case Else WhatIsMyLocation = "Unknown selection" End Select 'Memory cleanup Set oRange = Nothing End Function
Output

Next > Paste Special a complete reference in Excel VBA code example