1

I have an attribute table with 15 columns that contain either a '0' or a taxonomy number. I would like a summarized column that shows the number of different species.

location  type1  type2  type3  etc etc...  nr_of_species
    1      123    234    0      .........      2
    2      456     0     0      .........      1
    3       0      0     0      .........      0

Is there a way to do this with VBA and field calculator? I have no experience with VBA, so i might need a complete dummy-guide.

thanks!

1

UPDATE: I solved it with writing really ugly code:

Dim counter as integer
counter = 0

if [type1] > 0 then
    counter = counter + 1
endif
if [type2] > 0 then
    counter = counter + 1
endif

etc etc..

  • There is nothing wrong with the way you've done it, some logic can often be verbose. More importantly it works! :) – Hornbydd Nov 22 '13 at 13:41
1

If you wanted to use Python, you could do this in the Field Calculator...

In the "Pre-logic script code" box:

def CountFieldsGreaterThanZero(fieldList):
  counter = 0
  for field in fieldList:
    if field > 0:
      counter += 1
  return counter

In the expression box:

CountFieldsGreaterThanZero([!type1!, !type2!, !type3!, !type4!])

Essentially what you do is pass a "list of fields" (surrounded by brackets) that you want to evaulate to the CountFieldsGreaterThanZero function...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.