Reputation: 213
I'm trying to look for a specific text from my excel file to a SAP table.
I have tried below code but it gives me error "Invalid Next Control variable reference"
So far below is my code:
set rLastRow = rSheet.Cells(rSheet.rows.Count, "AO").End(xlUp).Row
Set Table = session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell")
rows = Table.RowCount - 1
cols = Table.RowCount - 1
Dim columns As Object
Set columns = Table.ColumnOrder
For i = 0 To rows
For j = 2 To rLastRow
If rSheet.Cells(j, "AO").Value = Table.GetCellValue(i, columns(3)) Then
MsgBox "Found!" & rSheet.Cells(j, "AO").Value & Table.GetCellValue(i, columns(3)), vbOKOnly
Else
'proceed to next value to find
End If
Next i
Next j
What I'm trying to do is for each cell in range "AO" in excel, it will look for a match in my GridView (SAP: fbl3n).
Is there another way where can I loop to each cell from range (excel) and look for it in a SAP table (GridView)?
Upvotes: 0
Views: 8379
Reputation: 1625
If the report is displayed as a grid, you could try the following:
Sub Test()
'
' Test Makro
'
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPApplication = SapGuiAuto.GetScriptingEngine
Set Connection = SAPApplication.Children(0)
Set session = Connection.Children(0)
rLastRow = ActiveCell.SpecialCells(xlLastCell).Row
Set Table = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell")
allRows = Table.RowCount - 1
Dim columns As Object
Set columns = Table.ColumnOrder
For i = 2 To rLastRow
'MsgBox Application.ActiveSheet.Cells(i, 41).Value
ActiveSheet.Cells(i, 41).Select
For j = 0 To allRows
'MsgBox Table.GetCellValue(j, columns(3))
If ActiveSheet.Cells(i, 41).Value = Val(Table.GetCellValue(j, columns(3))) Then
Table.setCurrentCell j, ""
Table.selectedRows = j
MsgBox "Found: " & ActiveSheet.Cells(i, 41).Value & " / " & Table.GetCellValue(j, columns(3)), vbOKOnly
End If
Next
Next
'
End Sub
Regards, ScriptMan
Upvotes: 2
Reputation: 1615
I don't know SAP, however I would
Set
in front of rLastRow becasuse rSheet.Cells(rSheet.rows.Count, "AO").End(xlUp).Row
returns a Long
. The reason of the error message may be here as Set
autodeclares Object
that can not be used for For
Option Explicit
rows
and columns
as variable name for Rows
and Columns
are keywords of VBA that may lead to confusionscheck the status after each Set
whether the assignment is OK, e.g.:
Set columns = Table.ColumnOrder
If columns is Nothing Then <...escape from here...>
Upvotes: 0