在 SAP table (gridview) 中查找范围的每个单元格值
Find each cell value of a range in SAP table (gridview)
我正在尝试从我的 excel 文件中查找特定文本到 SAP table。
我试过下面的代码,但它给我错误 "Invalid Next Control variable reference"
下面是我的代码:
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
我想要做的是针对 excel 中 "AO" 范围内的每个单元格,它会在我的 GridView (SAP: fbl3n) 中寻找匹配项。
是否有另一种方法可以让我从范围 (excel) 循环到每个单元格并在 SAP table (GridView) 中查找它?
我不知道 SAP,但我会
- 删除 rLastRow 前面的
Set
因为 rSheet.Cells(rSheet.rows.Count, "AO").End(xlUp).Row
returns 一个 Long
。错误消息的原因可能是因为 Set
autodeclares Object
that cannot be used for For
- 使用
Option Explicit
- 不使用
rows
和 columns
作为变量名 Rows
和 Columns
是 VBA 的关键字,可能会导致混淆
检查每个Set
后的状态是否赋值成功,例如:
Set columns = Table.ColumnOrder
If columns is Nothing Then <...escape from here...>
如果报表显示为网格,您可以尝试以下操作:
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
此致,
脚本人
我正在尝试从我的 excel 文件中查找特定文本到 SAP table。
我试过下面的代码,但它给我错误 "Invalid Next Control variable reference"
下面是我的代码:
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
我想要做的是针对 excel 中 "AO" 范围内的每个单元格,它会在我的 GridView (SAP: fbl3n) 中寻找匹配项。
是否有另一种方法可以让我从范围 (excel) 循环到每个单元格并在 SAP table (GridView) 中查找它?
我不知道 SAP,但我会
- 删除 rLastRow 前面的
Set
因为rSheet.Cells(rSheet.rows.Count, "AO").End(xlUp).Row
returns 一个Long
。错误消息的原因可能是因为Set
autodeclaresObject
that cannot be used forFor
- 使用
Option Explicit
- 不使用
rows
和columns
作为变量名Rows
和Columns
是 VBA 的关键字,可能会导致混淆 检查每个
Set
后的状态是否赋值成功,例如:Set columns = Table.ColumnOrder If columns is Nothing Then <...escape from here...>
如果报表显示为网格,您可以尝试以下操作:
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
此致, 脚本人