Hope this clears it up for you, so you can understand it a little better Brad.
(defun c:Test (/ ActDoc bAllNumbers Pos tempPos tempName)
(setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object)))
; get the active drawing
(vla-EndUndoMark ActDoc)
; end any undo mark that might be left open, pecautionary device
(vla-StartUndoMark ActDoc)
; start our own undo mark, so a single undo will undo the whole code
(if (ssget "x" '((0 . "INSERT")))
; if a valid selection set of inserts is gotten, then do what follows
(vlax-for obj (vla-get-ActiveSelectionSet ActDoc)
; lets step through the selection set of the active drawing, but as vla-objects instead of enames
(setq bAllNumbers T)
; set a variable that will let us know that all the characters following the underscore are numbers
(if
(and
(setq Pos (vl-string-search "_" (setq tempName (vla-get-Name obj))))
; if there is an underscore in the string set the variable Pos to the position of it
(setq tempPos (+ 2 Pos))
; vl-string-search starts at index 0, but substr starts at index 1, so we have to add 2 character positions
; to get the first character after the underscore
(progn
; we use progrn, because we only care that the last value, which will be returned, will be true, since we use an 'and' statement
(while (<= tempPos (strlen tempName))
; while our test position 'tempos' is less than or equal to the number of characters in the string, run what is below
(if (not (<= 48 (ascii (substr tempName tempPos 1)) 57))
; if the character is not a number, detrimed by if the ascii character value is between 48 and 57 or equal to the lowest or greatest
(setq bAllNumbers nil)
; if it's not a number, set our variable to nil
)
(setq tempPos (1+ tempPos))
; set our test postion to the next character
)
bAllNumbers
; return the check to see if they are all numbers after the underscore
)
(tblsearch "block" (substr tempName 1 Pos))
; this tests to make sure that the block we are going to rename the block to, if it passed all the tests before this one,
; exists in the drawing
)
; this will only be true only if all the tests are passed
(vla-put-Name obj (substr tempName 1 Pos))
; since it passed all the tests, change the name to be the name minus the underscore and everything after it.
)
)
)
(vla-EndUndoMark ActDoc)
; end our undo mark
(princ)
; exit quietly
)