Share via

Extract only first 10 rows

Anonymous
2022-04-22T16:27:48+00:00

Hi friends,

I've a code which copy data from SAP table from the top to bottom. If the table for each account have 1000 rows then this will copy each cell but may I know if the below code can be edited to copy only 10 rows only which saves a lot of time or copy rows when it finds specific text in 3rd column.

This is the code -

    Set myGrid = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell") 

    allRows = myGrid.RowCount - 1 'number of SAP rows 

    allCols = myGrid.ColumnCount - 1 'number of SAP Columns 

    Dim SAPCOL As Object 

    Set SAPCOL = myGrid.ColumnOrder 'SAP column names in order in SAP window 

     For J = 0 To allRows 

        myGrid.firstVisibleRow = J 

            For i = 0 To allCols 

            ActiveSheet.Cells(J + 1, i + 1).Value = myGrid.GetCellValue(J, SAPCOL(i)) 'starts in cell "A1" 

        Next i 

     Next J
Microsoft 365 and Office | Excel | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Anonymous
2022-04-22T16:45:59+00:00

Hi,

Your J loop starts from 0 . . so, 10 rows will be completed when J = 9 . . I have added a line that should exit the J loop when J = 10.

Hope it helps.

Set myGrid = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell")

allRows = myGrid.RowCount - 1 'number of SAP rows

allCols = myGrid.ColumnCount - 1 'number of SAP Columns

Dim SAPCOL As Object

Set SAPCOL = myGrid.ColumnOrder 'SAP column names in order in SAP window

For J = 0 To allRows

If J = 10 Then Exit For

myGrid.firstVisibleRow = J

For i = 0 To allCols

ActiveSheet.Cells(J + 1, i + 1).Value = myGrid.GetCellValue(J, SAPCOL(i)) 'starts in cell "A1"

Next i

Next J

If I was able to help You, please mark My response as answer and helpful.

Thank You!

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2022-04-22T18:36:49+00:00

    Thank you very much :) It's working. You are awesome.

    Was this answer helpful?

    0 comments No comments