Home arrow GIS Data & Resources arrow Scripts and Code arrow Visual Basic / VBA arrow VBA: Remove or Turn Off ArcMap Layers With Search String
VBA: Remove or Turn Off ArcMap Layers With Search String PDF Print E-mail

Written by Bert Granberg,

Building, calibrating and QC-ing linear referencing routes can add many layers to your arcmap project. Other projects and processes have the same issue: too many layers to manage with just mouse clicks.

If you keep your layer names consistent, it is easy to turn them on, off, or delete them from the ArcMap table of contents (TOC) programatically.

The code example below shows how to remove layers from the TOC using a search string. It also removes standalone tables too.

Public Sub removeLayersTables()
    
    Dim searchStr As String
    searchStr = "0015P"
    
    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Dim pTC As ITableCollection
    Dim pDS As IDataset
    Dim x As Integer
    
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    Set pTC = pMap
    
    For x = pMap.LayerCount - 1 To 0 Step -1
        If InStr(pMap.Layer(x).Name, searchStr) > 0 Then
            'pMap.Layer(x).Visible = false
            pMap.DeleteLayer pMap.Layer(x)
        End If
    Next x
    
    For x = pTC.TableCount - 1 To 0 Step -1
        Set pDS = pTC.Table(x)
        If InStr(pDS.Name, searchStr) > 0 Then
            pTC.RemoveTable pTC.Table(x)
        End If
    Next x

End Sub

Users' Comments  
 

No comment posted

Add your comment

22, Apr. 2009
 
< Prev   Next >

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for