Share via

VBA code that mimic Enable Selection

Anonymous
2023-06-26T11:30:20+00:00

I am reaching out to seek your expertise in creating a VBA code that replicates the functionality of the "Enable Selection" option in a pivot table.

Specifically, I would like to achieve the following behavior: whenever a cell is selected within a pivot table, I want all the corresponding data regions (DEPENDENT CESLLS) related to the selected cell to be highlighted in yellow.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

19 answers

Sort by: Most helpful
  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2023-06-26T13:46:07+00:00

    Target.PivotCell.PivotRow.Interior.Color = RGB(255, 255, 0)

    That's a fake code, PivotRow doesn't exists inside the object model. https://learn.microsoft.com/en-us/office/vba/api/excel.pivotcell#methods

    Try the code below.

    Andreas.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim Here As Range
    'In case there is not Pivot table
    On Error GoTo Exitpoint
    'Find the intersection of the whole Pivot table and the current row/column
    Set Here = Union( _
    Intersect(Target.EntireRow, Target.PivotTable.TableRange1), _
    Intersect(Target.EntireColumn, Target.PivotTable.TableRange1))
    'Select it
    Application.EnableEvents = False
    Here.Select
    Target.Activate
    Exitpoint:
    Application.EnableEvents = True
    End Sub

    Was this answer helpful?

    0 comments No comments
  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Anonymous
    2023-06-26T12:21:15+00:00

    Please note that I have Office Home & Business 2019

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2023-06-26T12:19:53+00:00

    Hello Anna, thank you so much for your reply.
    It gives an error: Run time error 438 Object doesn't support this property or this method.

    Target.PivotCell.PivotRow.Interior.Color = RGB(255, 255, 0)

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2023-06-26T12:09:57+00:00

    Hi Rami

    I'm AnnaThomas and I'd happily help you with your question. In this Forum, we are Microsoft consumers just like yourself.

    You can try this VBA code to create a macro that will highlight the corresponding data regions of a selected cell in a pivot table:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range) ' Check if the selected cell is in a pivot table If Not Intersect(Target, ActiveSheet.PivotTables(1). TableRange2) Is Nothing Then ' Clear any existing cell highlighting ActiveSheet.Cells.Interior.ColorIndex = xlNone ' Highlight the selected cell and its corresponding data regions Target.Interior.Color = RGB(255, 255, 0) Target.PivotCell.PivotRow.Interior.Color = RGB(255, 255, 0) Target.PivotCell.PivotColumn.Interior.Color = RGB(255, 255, 0) End If End Sub

    I hope this helps ;-), let me know if this is contrary to what you need, I would still be helpful to answer more of your questions.

    Best Regards,

    AnnaThomas

    Give back to the community. Help the next person with this problem by indicating whether this answer solved your problem. Click Yes or No at the bottom.

    Was this answer helpful?

    0 comments No comments