在SAP脚本中,是否可以通过名称而不是按行位置选择列?

时间:2018-10-23 14:58:52

标签: excel vba excel-vba sap

我有一个可以成功创建布局变体的脚本。通过选择列表中列名称所在的行号,可以选择要在报告中显示的列,如下所示:

 session.findById("wnd[1]/usr/tabsG_TS_ALV/tabpALV_M_R1/ssubSUB_DYN0510:SAPLSKBH:0620/cntlCONTAINER1_LAYO/shellcont/shell").selectedRows = "142"

由于在将来的某个时候,第142行中的项目可能会在列表中上移或下移,因此我想按其名称(在本例中为“订单”)进行选择。我已经尝试过使用.select或.selected的各种方法,但是没有找到任何有效的方法。理想情况下,它看起来像这样:

 session.findById("wnd[1]/usr/tabsG_TS_ALV/tabpALV_M_R1/ssubSUB_DYN0510:SAPLSKBH:0620/cntlCONTAINER1_LAYO/shellcont/shell").selectedName = "Order"

像这样可能吗?

2 个答案:

答案 0 :(得分:0)

可以从SAP列表框中进行选择,而无需知道哪一行包含要使用的字段。基本上,您可以按行号逐步浏览各行,并查看当前行的值是否等于要显示的字段。由于SAP为此使用了多个过程,因此该解决方案仅适用于简单的列表框。我有显示表的列表框的解决方案。如果要查看该解决方案,请告诉我。

当我第一次编写此代码时,我每次都从零行开始,然后转到列表框的底部以查找每个后续项。这是一个更高版本,该过程会根据预测下一个字段名称是按字母顺序上方还是下方的顺序,从当前行更改搜索方向。

为SAP列表框创建一个对象:

    Public gridView As Object

将SAP列表框放入gridView并调用子例程以浏览列表框中的项目。命名范围“ mnHead”是布局中所需的第一个字段名称的位置,其余字段在同一列中。

    Set gridView = session.findById("wnd[1]/usr/tabsG_TS_ALV/tabpALV_M_R1/ssubSUB_DYN0510:SAPLSKBH:0620/cntlCONTAINER1_LAYO/shellcont/shell")
    Call getFields("mnhead")

子例程:

Sub getFields(fNames As String)
Dim i, j, k, m, n As Integer
Dim str1, str2, str3, str4, str5 As String
j = 0                                                      ' j is counter for loop - it points at the current desired field name
k = 0                                                      ' k is start for the item to check
m = 1                                                      ' m is counter step 1 for alpha, -1 for anti-alpha
str3 = "0000000000000000"                                  ' padding for later compare
n = gridView.rowcount - 1                                  ' n = the number of rows in the listbox, it starts at row zero, so take one away
Do While Range(fNames).Offset(j, 0) <> ""                  ' loop to get all the fields
  str2 = Range(fNames).Offset(j, 0)                        ' put the desired field name in string var str2
  For i = k To n Step m                                    ' loop through the rows in the listbox
    gridView.currentCellRow = i                            ' scroll to the row number i
    gridView.SetCurrentCell i, "SELTEXT"                   ' select the item in row i
    If gridView.GetCellValue(i, "SELTEXT") = str2 Then     ' get the value in row I and see if it's the same as the field name wanted
      str1 = str2                                          ' it's the same so save the heading in temp variable str1
      Exit For                                             ' found so exit the loop
      End If
    Next i
  If i >= gridView.rowcount Then                           ' if we went all the way to the bottom with no match, give a message
    MsgBox ("Match not found for " & str2)
    Stop
    End If
  gridView.doubleClickCurrentCell                          ' double click on the row to send it to the left listbox
  str4 = Left(LCase(Left(Replace(str2, " ", ""), 14)) & str3, 15)
  str5 = Left(LCase(Left(Replace(Range(fNames).Offset(j + 1, 0), " ", ""), 14)) & str3, 15)
  If str4 <= str5 Then                                     ' continue alphabetically the list in next search
    If i < 0 Then k = 0 Else k = i                         ' check to see if we're below the first item and reset to first if we are
    m = 1                                                  ' count step is plus one
    n = gridView.rowcount - 1                              ' endpoint is the end of the alphabet
    Else
    If i > gridView.rowcount - 1 Then k = gridView.rowcount - 1 Else k = i      ' if we're past the end then reset to the end
    m = -1                                                 ' count step is minus one - anti-alpha
    n = 0                                                  ' endpoint is the beginning
    End If
  j = j + 1                                                ' increment j and reloop
  Loop

答案 1 :(得分:0)

我也需要这样做,因为我仅在布局中需要特定的列。因此,我选择了自己的布局,将其保存为一个变体,然后调用它来获取布局。与joe几乎相同的方法,但是要查找布局变体而不是列字段。

'open layout list
session.findById("wnd[0]/mbar/menu[5]/menu[2]/menu[1]").Select

'set list of all layouts
Set Layout = session.findById("wnd[1]/usr/ssubD0500_SUBSCREEN:SAPLSLVC_DIALOG:0501/cntlG51_CONTAINER/shellcont/shell")

'find Layout name in all the rows and choose it
iRows = Layout.RowCount()

For i = 0 To iRows - 1

  LayoutVariant = Layout.getCellValue(i, "VARIANT")

  If LayoutVariant = "/YourLayoutNameGoesHere" Then

    Layout.currentCellRow = i

    Layout.clickCurrentCell

    Exit For

  End If

Next
End Sub

对此SAP thread

进行了修改