Auto number title-block sheet number

This program numbers/renumbers the sheets in all the layouts.

It uses the layouts order to determine the numbering sequence, so make sure the layouts are in the desired order . Useful when new sheets are inserted or removed.
Commands:

AutoLISP code
Lisp
;;;This program numbers/renumbers the sheets in all the layouts.
;;;It uses the layouts order to determine the numbering sequence, so make sure the layouts are in ;;;the desired order . Useful when new sheets are inserted or removed.
;;;Use: Pick the sheet number attribute in the title-block  in one of the layouts and then enter ;;;the optional parameters (prefix and count start)

(vl-load-com)

(princ
  (strcat
    "\nProgram to auto-number block attributes in each layout in sequence with respect to layouts order."
    "\nCommands: "
    "\nLAYOUTS_AUTO_NR - Auto-number block attributes in each layout."
    "\n(c) "
    (substr (rtos (getvar "CDATE") 2 0) 1 4)
    " lisp-solutions.com"
  )
)
(princ)


(defun C:LAYOUTS_AUTO_NR (/	  att	  att_dx  blk	  blkname
			  bname	  doc	  ln	  offset  prefix
			  tag	  atts
			 )

  (if
    (and
      (setq
	att (car (nentsel "\nPick block attribute to autonumber>>"))
      )
      (setq att_dx (entget att))
      (= "ATTRIB" (cdr (assoc 0 att_dx)))
      (setq blk (cdr (assoc 330 att_dx)))
    )
     (progn
       (setq doc (vla-get-activedocument (vlax-get-acad-object)))
       (setq prefix (cond
		      ((getstring "\nPrefix:" t))
		      (t "")
		    )
       )
       (setq offset (cond
		      ((getint "\nCount starts from:"))
		      (t 0)
		    )
       )
       (setq bname (getpropertyvalue blk "BlockTableRecord/Name"))
       (setq tag (cdr (assoc 2 att_dx)))
       (vlax-for lay (vla-get-layouts doc)
	 (if (/= "Model" (vlax-get lay 'Name))
	   (progn
	     (setq ln (vla-get-TabOrder lay))
	     (vlax-for ob (vlax-get lay 'Block)
	       (if
		 (and (= "AcDbBlockReference" (vla-get-ObjectName ob))
		      (= bname
			 (getpropertyvalue
			   (vlax-vla-object->ename ob)
			   "BlockTableRecord/Name"
			 )
		      )
		      (= :vlax-true (vla-get-HasAttributes ob))
		      (setq atts (vlax-invoke ob 'GetAttributes))
		 )

		  (foreach att atts
		    (if
		      (= tag
			 (vlax-get att 'TagString)
		      )
		       (vlax-put att
				 'TextString
				 (strcat prefix (itoa (+ offset ln -1)))
		       )
		    )
		  )
	       )
	     )
	   )
	 )
       )
     )
  )

  (if (and doc (not (vlax-object-released-p doc)))
    (vlax-release-object doc)
  )
  (princ)
)

;;EOF