My workbook is essentially for Statistical Process Control
I have multiple worksheets - WorksheetA and WorksheetB , C , D ....
WorksheetA is where users enter / update new data manually
WorksheetB, C and the rest each contains 2 pivot tables - PivotTable1 and PivotTable2. PivotTable1 filters the raw data based on the product group from WorksheetA and displays the in process data dynamically. Data from PivotTable1 is then copied into other columns (say, Column A to C) in the same worksheet and an excel formula evaluates whether is there any out of control situation. If there is, then the value of the data will be displayed, otherwise it will show N/A.
As I would like all out of control situation to be logged down, PivotTable2 captures data in Column A to C and only logs down the data with a value (it means that they are out of control) and ignore those with N/A.
Now, I would like excel to display a textbook that says "You have new out of control situation" whenever PivotTable2 has new value. In Cell D1, I have a total count of the no. of data in PivotTable2. This is where VBA comes in :-
1) When the count in Cell D1 is updated with a new value, the textbox will automatically pop out. Here's the code :-
Private Sub Worksheet_Calculate()
Static countvalue
If Range("D1").Value <> countvalue Then
countvalue = Range("D1").Value
MsgBox "You have new out of control situation"
End If
End Sub
2) However, in order for the first portion to work, PivotTable1 and PivotTable2 have to auto-refresh whenever new values are entered (in order for Cell D1 count to be updated). Therefore, in WorksheetA, I have another data count in Cell F1 which records the total number of data in one of the columns (let's call this Column Z). Whenever someone manually enters new data in to Column Z, the count in Cell F1 changes and triggers this auto-refreshing formula :-
Private Sub Worksheet_Calculate()
Static autorefresh
If Range("F1").Value <> autorefresh Then
autorefresh = Range("F1").Value
Sheets("WorksheetB").PivotTables("PivotTable1").RefreshTable
Sheets("WorksheetB").PivotTables("PivotTable2").RefreshTable
End If
End Sub
These two groups of codes worked pretty well. However the only problem I face now is that whenever I save and reopen the workbook and enter new data in WorksheetA (but NOT in Column Z), the message box pops out even when the conditions for both worksheet_calculate are still not met. After this first msg box pop-out, everything returns normal again.
I'd appreciate it if anyone can shed some light to me on the codes that I'm using, and go through with me on what is wrong. I mostly referenced and combined the codes from various forums. I intend to replace the msg box with auto email sending capability whenever there is an out of control situation in the future. Therefore, if the worksheet_calculate function kept firing by itself whenever someone reopens the workbook, then false alarms will kept being sent to my email.
Thanks for your time reading through my questions. I appreciate it.