Trigger Macro with Slicer Change - excel

I'm trying to modify data in a cell range when changing the selection in a slicer.
Can this be done in Excel for Mac?

Yes, it is! As #BigBen had already mentioned, it is possible via the event Worksheet_PivotTableUpdate. This is also supported by Excel for Mac.
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
End Sub
It is important that the code is defined in the corresponding sheet module

Related

VBA Macro that Create Macro in Active Sheet

I found a way to highlight row when cell is selected. I would like to use it in every excel file I open but there is a problem. There are two steps. First, I have to create conditional formatting. Second, create a simple macro in the active sheet, not a normal module or personal.
My idea is that I create a personal macro that will do the first step (cond. form.) and then somehow create THE macro in the active sheet. But I don't know how.
Is it even possible?
This is the code I need to put into active sheeet module.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Target.Calculate
End Sub
Thanks.

Office 365 for Mac: Execute VBA macro on table change

I'm currently running Excel for Mac using the Office 365 subscription. I'm trying to execute a VBA macro whenever a certain cell value changes. I've already looked online and saw many code examples using the Worksheet_Change Sub, but it does not work for me however. This is how my code currently looks like:
Private Sub Worksheet_Change(ByVal Target as Range)
Target.Font.ColorIndex = 5
End Sub
I'm trying to run it on the second of four sheets. I clicked View Code on the second sheet so I am in the correct sheet though.
Does somebody know why it does/might not work? Does it not work in Excel for Mac?
If you need more information please tell me.
Thanks
I was having similar trouble earlier, and because your symptoms sound like mine (code not even executing) I suspect your code is actually not on the correct sheet. The VBA window in Mac is much less detailed than in Windows and I find it confusing.
Make sure that in the left-hand pane you click on the appropriate drop down
VBAProject(WorkbookName) -> Microsoft Excel Objects -> Sheet2
and insert the Worksheet_Change function there.
Also, if you want the color to change for only a certain range, you'll need to use Intersect:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will cause an alert when they are changed.
Set KeyCells = Range("A1:Z99")
'This part executes only if the cell being changed is in KeyCells
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
Target.Font.ColorIndex = 5
End If
End Sub

Double click in excel 2016

I am using excel 2016 with all protection set off or as low as possible. I want to make a double click on a cell.
I have pasted the code in "module 1", directly into the worksheet.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
MsgBox ("WHAT?")
End Sub
Put the code in Sheet1 or whatever one you want. These events are not fired in modules...
Worksheet_SomeEvent(...) are Worksheet functions so the code for such must be placed in worksheets in order to fire.

Automatically Run Macro When Data Is Pasted VBA

I have a worksheet where in the first three columns (A,B,C) I store data. These values are used in a macro.
I would like to know how it is possible to make this macro run automatically after data is pasted onto these columns. I am almost sure that I will use the Worksheet-Change module, but as for the code I am clueless.
Thanks in advance.
A simple implementation:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:C" & ThisWorkbook.Worksheets(1).UsedRange.Rows.Count)) Is Nothing Then
'Call your Macro to do stuff
End If
End Sub
Intersect checks if Target is in the range you want to monitor. So if something changes in columns past C, Intersect will return Nothing and your macro won't be called. Just keep in mind that the Worksheet_Change event fires on any change, even double clicking into the cells. If all you are doing in this Worksheet is copy&pasting data and then running your Macro, this should be fine, but if you are manipulating your data further, you might have to look at more sophisticated solutions. Examples include mirroring your Worksheet and comparing it pre/post Worksheet Changed Event. You can read more on this here: Detect whether cell value was actually changed by editing

Run VBA code automatically after running a filter

I've got a code written that categorizes employees along with their qualifications. In order to weed out employees with unwanted qualifications I have applied a filter to each column that titles the category of their qualification.
I've written my VBA code in order that repetitious names and qualifications are made invisible for ease of location. However, I am unable to get the code to run automatically.
Currently the only way I can get the code to run is by setting it to
Private Sub Worksheet_Change(ByVal Target As Range) and then changing the value of an arbitrary cell.
i found what I believe to be the correct solution at:
http://www.ozgrid.com/forum/showthread.php?t=72860
But I cannot make sense of it.
Is there a way to run this code without having to select and deselect a cell after the filter has run?
The key points from my article Trapping a change to a filtered list with VBA
There is more detail and a sample file with the article, the key points are summarised below
A "dummy" WorkSheet is added with a single SUBTOTAL formula in A1 pointing back to the range being filtered on the main sheet.
A Worksheet_Calculate() Event is added to the "dummy" WorkSheet, this Event fires when the SUBTOTAL formula updates when the filter is changed.
The next two setps are needed if it is desired to run the Workbook Calculation as Manual
Add a Workbook_Open Event to set the EnableCalculation property of all sheets other than "Dummy" to False.
Run the Workbook in Calculation mode
The ozgrid code you mentioned tells you that you can put your code in a worksheet_calculate event (in the worksheet module), as long as you have something that will recalculate when you change your autofilter. This something can be a subtotal formula that you can hide in your worksheet, e.g. =subtotal(3,A:A)
Still need to investigate but looks like Chart Calculate event is triggered when Calculation = xlCalculationManual. At least works on my Excel 2007. So the steps are:
create a chart (saying "Chart 1" on Sheet1) which actually uses data from any of your table column
check that it updates its picture when you change the filter
create a new class e.g. clsChartEvents:
Public WithEvents Chart As Chart
Private Sub Chart_Calculate()
Stop
End sub
add this code to some module or class:
Private chartEvents as new ChartEvents 'create a module-scope variable
sub SubscribeToChartEvents
set chartEvents.Chart = Sheet1.ChartObjects("Chart 1").Chart
end sub
execute SubscribeToChartEvents
change a filter and you should appear in Sub Chart_Calculate()

Resources