I figured why Antonio's code isn't working as it should...
When (entprev (entnext e))
(entnext e) is actually nested entity from entity e, so when supplied this kind of entity, this condition (eq EntNam (ssname SelSet Countr)) will never be true, so (while) loop is continuing to search until all entities from SelSet witch is actually (ssget "_X") are processed, as a result of while loop not finding match, counter is incrementing all the way, and this line (setq EntOut (ssname SelSet (1+ Countr)) EntNxt EntOut) will assign EntOut variable nil, because (ssname SelSet (sslength SelSet)) is always nil - ssname can find entities in range from [0 - (1- (sslength SelSet))]... So EntOut = nil => (entprev (entnext e)) => nil