My objective is that the quantity of the particular block needs to update automatically as per block count in model space my drawing contains more than 25-30 different blocks.
i used to show Legend/description and its quantity as per the below image.

that legend table I kept at layout/paper space.
given block work correctly but it also counts block which is in layout space. and I m having multiple sheets and going to put that table in all the sheets.
then this quantity also going to increase.
what I want is this code count only blocks that are in model space so in field text it shows the correct no. of the count.
as of now, I am using the method shown in the link which I have shared in an earlier msg.
here below I am pasting the code that I am going to use it.
(defun c:recount (/ block_list cnt env_name)
(setq block_list
(list
(cons '2 "S1")
(cons '2 "S2")
(cons '2 "S3")
(cons '2 "S4")
(cons '2 "S5")
(cons '2 "S6")
(cons '2 "S7")
(cons '2 "S8")
(cons '2 "S9")
(cons '2 "S10")
(cons '2 "S11")
(cons '2 "S13")
(cons '2 "S14")
(cons '2 "S15")
(cons '2 "S16")
(cons '2 "S17")
(cons '2 "S18")
(cons '2 "S19")
(cons '2 "S20")
(cons '2 "S21")
(cons '2 "NORMAL PUSH BUTTON")
(cons '2 "NORMAL PUSH BUTTON (W)")
(cons '2 "NORMAL PUSH BUTTON (FLAME PROOF)")
(cons '2 "LDB")
(cons '2 "ELDB")
(cons '2 "SPDB")
(cons '2 "ESPDB")
(cons '2 "ESPDB")
(cons '2 "DB")
(cons '2 "JB")
(cons '2 "MODULE_PLATE")
)
)
(foreach i block_list
(if
(setq ss (ssget "_X" '((0 . "INSERT")(410 . "Model"))))
(progn
(setq
cnt (itoa (sslength ss))
env_name (strcat "BLOCK_COUNT_" (cdr i))
)
(setenv env_name cnt)
(princ (strcat "\nSet " env_name " to " cnt))
)
(progn
(setq
cnt "0"
env_name (strcat "BLOCK_COUNT_" (cdr i))
)
(setenv env_name cnt)
(princ (strcat "\nSet " env_name " to " cnt))
)
)
)
(command "_REGEN") ;_refreshes field values
(princ)
)