问题
I have found out that a box popping up in relation to a pivottable is in fact an "alert" box as opposed to an "error" box. Does Excel have any kind of "On Alert GoTo :" like it does for "On Error GoTo :"???
I can disable the alert completely, but then I fail to warn users when they enter an incorrect name that isn't found on the pivottable. This also reassigns things and I don't want that!
回答1:
Preventing the Alert by Data Validation
One way to ensure that a user supplies a name from a predefined list is to use Excel's built-in data validation. You can have a cell provide its own drop-down list of choices that are based on a dynamic range. You can set this up from Excel's data tab and/or by using VBA. The reference is "dynamic" because, if the reference range changes, the drop-down list will change with it, and if the reference range is a column of a table, then the list will grow and shrink according to that column.
The action to take if validation fails can be customized as well, including an entry message and error message. One option when using VBA is to disable any action and instead use the validation's Value property to determine action at runtime. Validation is triggered before a cell value is changed, so my guess is that it will proceed the alert your seeing.
Errors vs Alerts (Can we work with an event?)
In regard to the more general question about if there is an On Error equivalent for alert messages, I don't think there is. VBA doesn't have stellar error handling to begin with. On Error handles runtime errors before message box display, not on a display event, and I don't think there are any VBA readily accessible events caused by message box display.
Errors vs Alerts (Can we work with an object?)
It sounds like you found Application.DisplayAlerts = False, and I think that's about as much as you can do with alerts. As the OP mentions, it would be nice to turn of alerts but still programatically check them. You can do this with errors by using the Err object, but I don't think you can do it with alerts if there's no alert object.
来源:https://stackoverflow.com/questions/34496888/does-excel-have-vba-for-on-alert-like-it-does-for-on-error