AO对选中要素进行属性统计
作者: 兔八哥
Public Sub SumSelectedFeatures()
Dim pMxDoc As IMxDocument
Dim pmap As Imap
Dim player As IFeatureLayer
Dim pFcc As IFeatureClass
Dim pFields As Ifields
Dim pNumFields As Ifields
Dim numAreaField As Double
Dim pField As Ifield
Set pMxDoc = ThisDocument
Set pmap = pMxDoc.FocusMap
Set player = pmap.Layer(0)
Set pFcc = player.FeatureClass
Set pFields = pFcc.Fields
'Get a field to Sum
Set pNumFields = pFields
numAreaField = pFields.FindField("pop1997") '
'Check for a valid field index number
If numAreaField < 0 Then
MsgBox "Please enter a Valid field name", vbCritical, "Field Doesn't Exist"
Exit Sub
End If
Set pField = pFields.Field(numAreaField)
'***Other useful field stuff***
'.FindField("AREA")
'MsgBox numAreaField
'MsgBox pField.Name
'MsgBox pFields.FieldCount
'MsgBox player.Name
'Get the selected records
Dim pFeatureSelection As IFeatureSelection
Set pFeatureSelection = player
Dim pSelected As ISelectionSet
Set pSelected = pFeatureSelection.SelectionSet
Dim pCursor As Icursor
pSelected.Search Nothing, False, pCursor
Dim pfeature As Ifeature
Dim counter As Integer
counter = 0
Dim sumAREA As Double
sumAREA = 0
Set pfeature = pCursor.NextRow
Do Until pfeature Is Nothing
counter = counter + 1
sumAREA = sumAREA + pfeature.Value(numAreaField)
Set pfeature = pCursor.NextRow
Loop
MsgBox "Total " & pField.Name & " is: " & sumAREA
'MsgBox counter & " Selected records"
End Sub