Select to view content in your preferred language

Count only selected features

4504
3
12-14-2010 11:57 AM
MikeLouwrens
Frequent Contributor
I am wanting my tool to run only on selected features - so if there are no features selected (manually using the Selection tool within ArcMap) the tool won't run.

How do I count the number of selected features, or find if the number selected is greater than 0?

Currently doing this in VBA (ArcGIS 10).

Cheers,
Mike.
0 Kudos
3 Replies
MikeLouwrens
Frequent Contributor
Haha if only I'd read through my other code in VBA - I've done this before, only a few months ago 🙂

Here's all I needed
    Dim pDisplayTable As IDisplayTable
    Set pDisplayTable = pFeatLayer
 
    Dim pSelSet As ISelectionSet
    Set pSelSet = pDisplayTable.DisplaySelectionSet
 
    Dim pCount As Integer
    pCount = pSelSet.count
 
    If pCount = 0 Then
        MsgBox "Nothing Selected"
        Exit Sub
    End If


Cheers,
Mike.
0 Kudos
FrankKish
Esri Contributor
You could also use "pMxdoc.FocusMap.SelectionCount"

Such as in ...

Private Sub main()
    Dim pMxdoc As IMxDocument
    Set pMxdoc = ThisDocument
    If pMxdoc.FocusMap.SelectionCount = 0 Then
        MsgBox "No feature selected...Exiting"
        Exit Sub
    End If
    Dim pEnumFeature As IEnumFeature
    Set pEnumFeature = pMxdoc.FocusMap.FeatureSelection
    Dim pF As IFeature
    Set pF = pEnumFeature.Next
    Do Until pF Is Nothing
        ''do something with each feature
        Set pF = pEnumFeature.Next
    Loop
End Sub
0 Kudos
MikeLouwrens
Frequent Contributor
You could also use "pMxdoc.FocusMap.SelectionCount"

Not quite, as this gives total selected features within the map document, not per layer (I just realised I hadn't specified that in my OP) - my script loops through all layers, and runs on the layer if there are selected features in that layer.

Thanks for your reply,
Mike.
0 Kudos