I have a problem that is breaking my head for a couple of days now, so I decided to share it with you, maybe you already stumbled upon the same problem.
=== this is the context: ===
I'm working on this excel file where I have, among other, these three tabs: - one, called "Data", where I store the data I need for the file in a regular table; - the second, called "Chart_1", where one can find pivot_table_1 and chart_1. The source for these objects is in "Data" tab. - the third tab is called "Chart_2" and contains pivot_table_2 and chart_2. These objects are also sourced from "Data" tab. I have five slicers in tab "Chart_1" and another five in tab "Chart_2". All the slicers are connected to both pivot_tables found in the "Chart*" tabs. I used VBA for the charts formats in the tabs, as Worksheet_Change events.
=== this is the problem: ===
When I select something in the slicer (whichever is the "Chart*" tab), it always activates "Chart_2". If I click in one of the slicers in tab "Chart_1", when the macros finish running, tab "Chart_2" is activated. When I click in one of the slicers in tab "Chart_2", everything is fine because the activated tab doesn't change. Whichever slicer I use (whatever tab is it embedded in), VBA always runs first "Chart_1"'s Worksheet_Change and then "Chart_2"'s Worksheet_Change. I think this is happening because of the filter connections: when I click in one slicer, it will trigger all affected Worksheet_Change events.
=== this is what I already tried: ===
So, this is my logic/programming problem, right now. Do you have any suggestion on how can I fix it? I just want to stay in the tab where I was when I clicked the slicer...
As discussed in the comments:
Chart1-Sheet:
Public GoodNameForAVariable As String
Private Sub Worksheet_Change(ByVal Target As Range)
GoodNameForAVariable = ActiveSheet.Name
'Your code happens here
End Sub
Chart2-Sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
'Your code happens here
ThisWorkbook.Sheets(GoodNameForAVariable).Activate
End Sub