Dynamically count fields in an internal table

The code in this tip enables you to check how many fields there are in an itab in runtime.

The code below enables you to check how many fields there are in an itab in runtime. The code is one function module.

The code was written in R/3 4.6c.


FUNCTION ZMANDY_EXCEL_UPLOAD.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     REFERENCE(FILE_NAME) TYPE  RLGRAP-FILENAME DEFAULT
*"       'C:WINDOWSDESKTOP'
*"  TABLES
*"      INTERNAL_TAB
*"----------------------------------------------------------------------

INCLUDE OLE2INCL.

DATA: STR(20),NDATE(10).
DATA: ROW TYPE I VALUE 1, COL TYPE I VALUE 1.
DATA OBJ TYPE OLE2_OBJECT.
DATA WORKBOOK TYPE OLE2_OBJECT.
DATA SH TYPE OLE2_OBJECT.
DATA CELL TYPE OLE2_OBJECT.
DATA RANGE TYPE OLE2_OBJECT.
DATA APP TYPE OLE2_OBJECT.
DATA SEL TYPE OLE2_OBJECT.
DATA COLUMNS TYPE OLE2_OBJECT.
DATA TYP.
FIELD-SYMBOLS: <F>.

CREATE OBJECT OBJ 'excel.application'.
SET PROPERTY OF OBJ 'visible' = 1.
CALL METHOD OF OBJ 'Workbooks' = WORKBOOK.
WRITE :/ 'workbook', SY-SUBRC.
CALL METHOD OF WORKBOOK 'add'.
CALL METHOD OF OBJ 'Worksheets' = SH EXPORTING #1 = 1.
CALL METHOD OF SH 'Activate'.



LOOP AT INTERNAL_TAB.
COL = 1.
do.
ASSIGN COMPONENT SY-INDEX OF STRUCTURE INTERNAL_TAB TO <F>.
IF SY-SUBRC <> 0.
EXIT.
ENDIF.
DESCRIBE FIELD <F>. "TYPE TYP COMPONENTS N.
CALL METHOD OF SH 'Cells' = CELL EXPORTING #1  = ROW #2 = COL.
SET PROPERTY OF CELL 'value' = <F>. "VALUE.
*PERFORM INSTERDATA USING ROW COL <F>.
COL = COL + 1.
ENDDO.
ROW = ROW + 1.
ENDLOOP.
STR = ROW.
ENDFUNCTION.

This was first published in November 2002

Dig deeper on SAP ABAP

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close