HOW TO

Find a layer by name using ArcObjects

Last Published: December 6, 2022

Summary

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

Procedure

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

Article ID:000004885

Software:
  • ArcMap 8 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic