This sample consists of a VBA macro that updates unique values symbology for a layer by looking at the data currently "behind" the layer. Essentially, the macro automates the manual process of removing "Zero count" values from a unique values symbology without disturbing the match between each individual value and symbol. In ArcObjects lingo, Values are removed from the UniqueValuesRenderer that are not present in the current data. Removing values from the renderer means that they will no longer appear in the ArcMap Table of Contents and Legend. This is particularly useful: a) if the data behind the layer has been edited b) if the layer now points to new data (a new feature class), or c) to synchronize unique values symbology imported from a layer file (.lyr) with the currently referenced data. Note: the logic only removes values from the renderer that are not present in the reference feature class, it does not add any new values to the renderer that it might find in the feature class. Also, layer definition queries are respected.
How to use:
Open ArcMap and create a new map document.
Use the 'Add Data' button to add ..\arcgis\arcexe83\ArcObjects Developer Kit\Samples\Data\Usa\states.shp to your map.
Set up Unique values symbology based on the 'SUB_REGION' field. Add all values and check off the 'All other values' class. Press Apply.
To demonstrate, set a Definition query (Layer properties dialog > Definition Query tab) that removes several values. For example, use this query: "SUB_REGION" <> 'Pacific' AND "SUB_REGION" <> 'Mtn'. Press Ok.
Create a new macro (Tools--> Macros--> Create). Copy the code provided
in RemoveUniqueValsNotPresent.bas and paste into the Visual Basic Editor of your map document.
Run the macro. 'Pacific' and 'Mtn' are removed from the Table of Contents.
An ArcMap session with states.shp loaded.