How To: Zoom to selected features at a specified scale


Information provided describes how to zoom in on a selected feature in ArcMap at a specified scale.


  1. In ArcMap, navigate to Tools > Macros > Visual Basic Editor.
  2. On the left side of the Visual Basic Editor window, expand either the Normal (Normal.mxt) or the Project item.
  3. Expand the ArcMap Objects item.
  4. Select the ThisDocument module, right-click and select View Code.
  5. Copy and paste the following code:

    'zoom to selected feature at specified scale
    Sub ZoomSelectedToScale()

    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument
    Dim pMap As IMap
    Set pMap = pDoc.FocusMap

    Dim pLayer As IFeatureLayer
    Dim pFSel As IFeatureSelection
    Set pLayer = pMap.Layer(0)
    Set pFSel = pLayer

    'Get the selected features
    Dim pSelSet As ISelectionSet
    Set pSelSet = pFSel.SelectionSet

    Dim pEnumGeom As IEnumGeometry
    Dim pEnumGeomBind As IEnumGeometryBind

    Set pEnumGeom = New EnumFeatureGeometry
    Set pEnumGeomBind = pEnumGeom
    pEnumGeomBind.BindGeometrySource Nothing, pSelSet

    Dim pGeomFactory As IGeometryFactory
    Set pGeomFactory = New GeometryEnvironment

    Dim pGeom As IGeometry
    Set pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom)

    pDoc.ActiveView.Extent = pGeom.Envelope
    pMap.MapScale = 2000 'change this value to the desired scale

    End Sub

    A shortcut button can be created to execute this code.

    A. Select Tools > Customize to open the Customize dialog box.
    B. Select the Commands tab.
    C. Select [UIControls] from the Categories list box.
    D. Select Untitled from the Save In drop-down list to save the button to this map document. Select Normal to save the button to all ArcMap documents on the machine.
    E. Click New UIControl.
    F. Select UIButtonControl and click Create.
    G. Drag the new UIButtonControl to the toolbar of choice.
    H. Close the Customize dialog box.

  6. Change the value of 2000 in the line 'pMap.MapScale = 2000' to the desired scale.
  7. Go back to ArcMap. If necessary, add a layer first. Select a feature from the top layer using the Select Features tool.
  8. Execute the code by performing one of the following:
    · Go to Visual Basic Editor. Click anywhere in the body of the code. Press F5.
    · If a UIButtonControl was created to run the code, click the button.

Related Information