How to deal with SAP tables – part 1

Recording a value selection from the table in SAP is easy. You just need to record specific cell coordinates. It is different, when the data items in the table change frequently. In today’s article I’m going to show You how to deal with SAP tables.

How to deal with SAP tables - part 1

This is the first part of this article, where I’m going to show You how to deal with tables in SAP on examples.

I will not present here how to log into the SAP.
If You want to know how to do this go here.

Table I

In this example I selected the last row, in the last column of the table. The recorded code looks like this.

session.findById("wnd[0]/usr/cntlGRID_CONT0050/shellcont/shell").setCurrentCell 3, "SGTITLE"

As You can see, there are 2 red brackets in 4th row and 6th column, behind blue rectangle (safety reasons). The last cell is selected by setCurrentCell. You can see in the code that the column name is SGTITLE and row number is 3.
Remember, that enumeration in SAP tables always starts with 0!

How to deal with SAP tables - part 1 table

Just by looking at the table we know, that it has 4 rows and 6 columns. The easiest way to see all the table properties is to go to the Watches.

How to deal with SAP tables - part 1 watches

Thanks to the ColumnOrder property we know the rest of the column names.

How to deal with SAP tables - part 1 table column names

In case of table objects, having as the last in the name /shell, You can always use ColumnCount to count the amount of the columns and RowCount to count the rows.

lastRow = session.findById("wnd[0]/usr/cntlGRID_CONT0050/shellcont/shell").RowCount
col_ctn = session.findById("wnd[0]/usr/cntlGRID_CONT0050/shellcont/shell").ColumnCount

Then gather all the column names in array. To do that use simple loop and get all the items of ColumnOrder property (just like You saw them in the Watches screenshot).

For j = 0 To colOrd_ctn - 1
    colOrd_arr(j) = session.findById("wnd[0]/usr/cntlGRID_CONT0050/shellcont/shell").ColumnOrder.Item(j)
Next

Now You have all the information to easily loop through all cells of that SAP table. In this example we are going to create array table.

To get the cells value You can use .getCellValue, where You need to pass number of row and column name.

ReDim SAPTable_arr(0 To lastRow - 1, 0 To col_ctn - 1)

For j = 0 To col_ctn - 1
    For i = 0 To lastRow - 1
        SAPTable_cell = session.findById("wnd[0]/usr/cntlGRID_CONT0050/shellcont/shell").getCellValue(i, colOrd_arr(j))
        SAPTable_arr(i, j) = SAPTable_cell
    Next
Next
How to deal with SAP tables - part 1 watches array

Of course You don’t have to duplicate the table with array, looping through You can check columns or rows to find certain data.

Code

After all description here comes the whole code together.

'Connect to SAP
'SAP transaction recording
'...

session.findById("wnd[0]/usr/cntlGRID_CONT0050/shellcont/shell").setCurrentCell 3, "SGTITLE"

Dim i As Long, j As Long, col_ctn As Long, lastRow As Long
Dim colOrd_arr As Variant, SAPTable_arr As Variant
Dim SAPTable_cell As String

lastRow = session.findById("wnd[0]/usr/cntlGRID_CONT0050/shellcont/shell").RowCount
col_ctn = session.findById("wnd[0]/usr/cntlGRID_CONT0050/shellcont/shell").ColumnCount
ReDim colOrd_arr(0 To col_ctn - 1)

For j = 0 To col_ctn - 1
    colOrd_arr(j) = session.findById("wnd[0]/usr/cntlGRID_CONT0050/shellcont/shell").ColumnOrder.Item(j)
Next

ReDim SAPTable_arr(0 To lastRow - 1, 0 To col_ctn - 1)

For j = 0 To col_ctn - 1
    For i = 0 To lastRow - 1
        SAPTable_cell = session.findById("wnd[0]/usr/cntlGRID_CONT0050/shellcont/shell").getCellValue(i, colOrd_arr(j))
        SAPTable_arr(i, j) = SAPTable_cell
    Next
Next

Table I – summary

It was not so hard as it seemed at the beginning. In the end I was surprised that in the past I just didn’t know what to do at all.

That was the first example, I got for You one more case of table with check boxes. So stay tuned for the second part of how to deal with SAP tables.

Tomasz Płociński

Author: Tomasz Płociński

Excel VBA enthusiast who is also open for other languages. Mainly working in VBA, some SQL, hungry for more.

One thought on “How to deal with SAP tables – part 1”

Leave a Reply

Your email address will not be published. Required fields are marked *