Situation:
I am using a PivotTableUpdate
event to clear a range to the right of a PivotTable
and then insert values, in the next available column, to the right of the PivotTable.DataBodyRange
.
Single slicer item selection:
In the first image below, I am selecting one slicer item and then using
.Offset(, .Columns.Count).Resize(.Rows.Count, 25)
to clear a region to the right of the DataBodyRange
. The Resize
is to account for prior selections which may have populated a different column from the currently next available.
I am then using
.Offset(, .Columns.Count).Resize(.Rows.Count, 1) = "1"
to set the next available free column's value to, in this case, 1
.
Example: One slicer item selection, table and pivot in Sheet1:
So I am going from "before" to "after" as shown below:
This works fine. There is no change in the next available column address.
Multi-item slicer selection:
When I attempt the same thing with a multi-select the code halts with the standard "Alert before overwriting cells" i.e. I get:
There is already data in..Do you want to replace it?
Example: Multi slicer item selection, table and pivot in Sheet1:
What I have tried:
Application.DisplayAlerts = False
And
Application.AlertBeforeOverwriting = False
And (to clear the pop-up)
Application.SendKeys "{ENTER}"
The message still appears for multi-select and doesn't go away until I manually press the OK
Thoughts:
I had thought perhaps the message is triggered before the event code is run, but I don't get this message when I overwrite the same range (i.e. single slicer item selection)
I know you can manually switch this alert off via the File > Options > Advanced Settings but am unsure how to suppress, via code, in my current code.
Anyone have a solution on how to suppress this alert?
Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Application.DisplayAlerts = False
With Target.DataBodyRange
Dim clearRange As Range
Set clearRange = .Offset(, .Columns.Count).Resize(.Rows.Count, 25)
clearRange.ClearContents
.Offset(, .Columns.Count).Resize(.Rows.Count, 1) = "1"
End With
Application.DisplayAlerts = True
End Sub
Test data:
| Trust | STP | Val |
|-------|------|-----|
| A | STPa | 1 |
| B | STPb | 2 |
| C | STPc | 3 |
References: