HOW TO
This VBA sample shows how to get a reference to a layer by name rather than an index number.
Note: Support for Visual Basic for Applications (VBA) for ArcMap and ArcCatalog ended with the ArcGIS 10.2.2 release, and Esri has not included VBA compatibility setups since version 10.5. See: ArcGIS Desktop and VBA Moving Forward
Loop through all the layers in the map using IEnumLayer to check the layer names.
Note: This article applies to retired ArcGIS versions 8.x. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands and geoprocessing tools.
Sub GetLayerByName() Dim pDoc As IMxDocument Set pDoc = ThisDocument ' Get all the layers in the Map Dim pEnumLayer As IEnumLayer Set pEnumLayer = pDoc.FocusMap.Layers ' Loop through all the layers Dim pLayer As ILayer Set pLayer = pEnumLayer.Next Do Until pLayer Is Nothing If pLayer.Name = "STATES" Then Exit Do End If Set pLayer = pEnumLayer.Next Loop End Sub
Get help from ArcGIS experts
Download the Esri Support App