在SAP表(gridview)中查找范围的每个单元格值

时间:2018-02-15 05:04:37

标签: vba excel-vba sapscript excel

我试图从excel文件中查找特定文本到SAP表。

我试过下面的代码,但是它给了我错误"无效的下一个控件变量引用"

以下是我的代码:

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

我想要做的是针对范围内的每个细胞" AO"在excel中,它将在我的GridView(SAP:fbl3n)中查找匹配项。

还有另一种方法可以从范围(excel)循环到每个单元格并在SAP表格中查找它(GridView)吗?

2 个答案:

答案 0 :(得分:1)

如果报告显示为网格,您可以尝试以下操作:

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

此致 ScriptMan

答案 1 :(得分:0)

我不了解SAP,但我会

  1. 删除rLastRow前面的Set,因为rSheet.Cells(rSheet.rows.Count, "AO").End(xlUp).Row会返回Long。出现错误消息的原因可能是Set autodeclares Object,不能用于For
  2. 使用Option Explicit
  3. 不使用rowscolumns作为Rows的变量名称,而Columns是可能导致混淆的VBA关键字
  4. 检查每个Set后的状态是否正确,例如:

    Set columns = Table.ColumnOrder
    If columns is Nothing Then <...escape from here...>