How to delete map grids


You can use this sample's code to delete all of the map grids in the focus map.

How to use

  1. Copy-paste this sample's code into a module in your Visual Basic Editor.
  2. Run the procedure. This will remove all map grids from the currently selected data frame.
[VBA]
Public Sub DeleteMapGrids()
    Dim pMxDoc As IMxDocument
    Dim pMap As IMap
    Dim pActiveView As IActiveView
    Dim pGraphicsContainer As IGraphicsContainer
    Dim pMapFrame As IMapFrame
    Dim pMapGrids As IMapGrids
    Dim pMapGrid As IMapGrid
    
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    Set pActiveView = pMxDoc.PageLayout
    Set pGraphicsContainer = pMxDoc.PageLayout
    Set pMapFrame = pGraphicsContainer.FindFrame(pMap)
    Set pMapGrids = pMapFrame
    
    Dim i As Long
    Dim Count As Long
    
    ' Delete all mapgrids and refresh
    Count = pMapGrids.MapGridCount
    For i = Count - 1 To 0 Step -1
        'if you remove grid(0) first, then grid(1) becomes grid(0)
        Set pMapGrid = pMapGrids.MapGrid(i)
        pMapGrids.DeleteMapGrid pMapGrid
        Set pMapGrid = Nothing
    Next i
    pActiveView.PartialRefresh esriViewBackground, Nothing, Nothing
End Sub