Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

My collect does not work. please help....

aris_hidalgo
Contributor
0 Kudos

Hello experts,

I am wondering what I am doing wrong here. Based on the logic below, I want to write the results of subtotal and grand total based on my collect. But when I run my report it does not work. it does not collect the values. Below is my code:

it_finaltab TYPE STANDARD TABLE OF t_finaltab WITH HEADER LINE,

wa_finaltab type standard table of t_finaltab with header line.

FORM display_results.

SORT it_finaltab BY funcloc asset_dum DESCENDING

asset DESCENDING parent DESCENDING.

*local variables

DATA: lv_color TYPE i VALUE 1,

lv_subtot(1),

lv_total(1),

lv_flag(1),

lv_lines TYPE i,

subtot LIKE anlc-kansw,

total LIKE anlc-kansw,

gtotal LIKE anlc-kansw,

lv_old LIKE iloa-tplnr,

lv_new LIKE iloa-tplnr.

*check number of records in itab

DESCRIBE TABLE it_finaltab LINES lv_lines.

IF lv_lines > 1.

lv_flag = 1.

ENDIF.

*write results

LOOP AT it_finaltab.

COLLECT it_finaltab into wa_finaltab.

AT END OF asset_dum.

lv_subtot = 1.

ENDAT.

AT END OF funcloc.

lv_total = 1.

ENDAT.

lv_new = it_finaltab-funcloc.

IF NOT lv_old IS INITIAL.

IF lv_new <> lv_old.

NEW-PAGE.

FORMAT COLOR COL_HEADING.

WRITE: / sy-uline(sy-linsz) NO-GAP,

sy-vline,

(17) 'Asset' CENTERED,

(17) 'Parent' CENTERED,

(40) 'Description' CENTERED,

(15) 'Asset Sub-class' CENTERED,

(40) 'Location' CENTERED,

(15) 'Cost' CENTERED,

(20) 'Acc. Depreciation' CENTERED,

(20) 'Net Book Value' CENTERED,

sy-vline,

sy-uline(sy-linsz) NO-GAP.

FORMAT COLOR OFF.

ENDIF.

ENDIF.

lv_old = lv_new.

IF lv_color = 1.

lv_color = 2.

ELSE.

lv_color = 1.

ENDIF.

FORMAT INTENSIFIED OFF COLOR = lv_color.

WRITE: / sy-vline,

(17) it_finaltab-asset_subnum CENTERED,

(17) it_finaltab-parent_subnum CENTERED,

(40) it_finaltab-description CENTERED,

(15) it_finaltab-asset_sub CENTERED,

(40) it_finaltab-location CENTERED,

(15) it_finaltab-accq_cost CENTERED,

(20) it_finaltab-acc_dep CENTERED,

(20) it_finaltab-asset_book_val CENTERED,

sy-vline.

  • ADD: it_finaltab-asset_book_val TO subtot,

  • it_finaltab-asset_book_val TO gtotal.

*write subtotal

**if there is only one record in itab, subtotal would not be displayed

IF lv_subtot = 1 AND lv_flag = 1.

CLEAR lv_subtot.

FORMAT COLOR COL_TOTAL.

WRITE: / sy-vline,

'Sub-total:',

AT 137(15) wa_finaltab-accq_cost CENTERED,

AT 153(20) wa_finaltab-acc_dep CENTERED,

AT 174(20) wa_finaltab-asset_book_val CENTERED,

sy-vline.

  • CLEAR subtot.

FORMAT COLOR OFF.

ENDIF.

*write grand total

IF lv_total = 1.

FORMAT COLOR COL_TOTAL INTENSIFIED ON.

WRITE: / sy-uline(sy-linsz),

sy-vline,

'Grand Total:', it_finaltab-funcloc,

AT 137(15) wa_finaltab-accq_cost CENTERED,

AT 153(20) wa_finaltab-acc_dep CENTERED,

AT 174(20) wa_finaltab-asset_book_val CENTERED,

sy-vline,

sy-uline(sy-linsz).

CLEAR gtotal.

FORMAT COLOR OFF.

ENDIF.

ENDLOOP.

ENDFORM.

By the way, I tried using add but it is tedious so I am experementing with collect statement. Again, thank you guys and take care!

Message was edited by: viraylab

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Viraylab,

U can refer the following code.

types: begin of wa_vbap,

matnr like vbap-matnr,

uepos like vbap-uepos,

mvgr1 like vbap-mvgr1,

mvgr2 like vbap-mvgr2,

mvgr3 like vbap-mvgr3,

serail like vbap-serail,

updkz like vbapvb-updkz,

kwmeng like vbap-kwmeng,

end of wa_vbap.

types: begin of wa_vbapx,

matnr like vbap-matnr,

kwmeng like vbap-kwmeng,

end of wa_vbapx.

data t_vbap type table of wa_vbapx with header line.

loop at xvbap.

move-corresponding xvbap to t_vbap.

collect t_vbap.

endloop.

Reward points if helpful.

Rgds,

Sumana

5 REPLIES 5

Former Member
0 Kudos

The internal table in which u collect,should have

on the LEFT MOST,only matnr and werks.

rest the system will automatically sum up based on their distinct combination

Former Member
0 Kudos

'Collect' sums up the numeric values based on all the non-numeric fields. See to it that your internal table has only those fields on which you want to do the sum and the amount fields.

former_member480923
Active Contributor
0 Kudos

Try using ADD between AT END OF <field> and ENDAT.

For the collect statemnet to be usefull all the fields BEFORE the numeric fields should be Character fields.

hope it helps ............ pls award points

Anirban

Former Member
0 Kudos

Hi Viraylab,

U can refer the following code.

types: begin of wa_vbap,

matnr like vbap-matnr,

uepos like vbap-uepos,

mvgr1 like vbap-mvgr1,

mvgr2 like vbap-mvgr2,

mvgr3 like vbap-mvgr3,

serail like vbap-serail,

updkz like vbapvb-updkz,

kwmeng like vbap-kwmeng,

end of wa_vbap.

types: begin of wa_vbapx,

matnr like vbap-matnr,

kwmeng like vbap-kwmeng,

end of wa_vbapx.

data t_vbap type table of wa_vbapx with header line.

loop at xvbap where updkz ne 'D'.

move-corresponding xvbap to t_vbap.

collect t_vbap.

endloop.

Reward points if helpful.

Rgds,

Sumana

Former Member
0 Kudos

Hi Viraylab,

U can refer the following code.

types: begin of wa_vbap,

matnr like vbap-matnr,

uepos like vbap-uepos,

mvgr1 like vbap-mvgr1,

mvgr2 like vbap-mvgr2,

mvgr3 like vbap-mvgr3,

serail like vbap-serail,

updkz like vbapvb-updkz,

kwmeng like vbap-kwmeng,

end of wa_vbap.

types: begin of wa_vbapx,

matnr like vbap-matnr,

kwmeng like vbap-kwmeng,

end of wa_vbapx.

data t_vbap type table of wa_vbapx with header line.

loop at xvbap.

move-corresponding xvbap to t_vbap.

collect t_vbap.

endloop.

Reward points if helpful.

Rgds,

Sumana