Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Logic to add 1 from 01 until 99, then go to A1->Z9

I have a requirement add 1 to the line no. whenever I add a record to the table. I have to search the table for the highest line number saved and add 1 to it. The trick is that when I get to 99, the next line number should be A1, and then A2 until A9. When I get to A9 I should go to B1, etc. until Z9.

How can I accomplish this?

Former Member
Former Member replied

Hi

Perhaps something like this can help you:

DATA: BEGIN OF ITAB OCCURS 0,

        KEY(2) TYPE C,

       END   OF ITAB.

DATA: NUM_CHECK TYPE I.

DATA: NUM2(2) TYPE N.

DATA: NUM(1) TYPE N.

DATA: OFFSET TYPE I.

PARAMETERS: P_TIMES TYPE I.

DO P_TIMES TIMES.

   SORT ITAB BY KEY DESCENDING.

   READ TABLE ITAB INDEX 1.

   IF SY-SUBRC <> 0.

     ITAB-KEY = '01'.

   ELSE.

     IF ITAB-KEY CO '0123456789'.

       MOVE ITAB-KEY TO NUM_CHECK.

       IF NUM_CHECK < 99.

         NUM_CHECK = NUM_CHECK + 1.

         MOVE NUM_CHECK TO NUM2.

         MOVE NUM2      TO ITAB-KEY.

       ELSE.

         ITAB-KEY = 'A1'.

       ENDIF.

     ELSE.

       MOVE ITAB-KEY+1 TO NUM.

       IF NUM < 9.

         NUM = NUM + 1.

         ITAB-KEY+1(1) = NUM.

       ELSE.

         OFFSET = OFFSET + 1.

         ITAB-KEY(1) = SY-ABCDE+OFFSET(1).

         ITAB-KEY+1(1) = '1'.

       ENDIF.

     ENDIF.

   ENDIF.

   APPEND ITAB.

ENDDO.

SORT ITAB BY KEY.

LOOP AT ITAB.

   WRITE: / ITAB-KEY.

ENDLOOP.

Max

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question