English

How To: Find a layer by name using ArcObjects

Summary

This sample shows how to get a reference to a layer by name rather than an index number.

Procedure

Loop through all the layers in the map using IEnumLayer to check the layer names.

Code:
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