如何在 SAP 中使用和循环 Excel 变量

How to use and loop Excel variables in SAP

我想在 excel 中创建一个循环,循环遍历单元格并将它们设置为变量并在 SAP 中使用该变量。我在单元格 A46 中有第一个变量。

Sub CustomList()
    Dim LR As Long, i As Long
    Range("A46").Select

    Do
        j = ActiveCell.Value

        Call SAPExportCustom

        ActiveCell.Offset(1).Activate     'Move one cell down
    Loop Until ActiveCell.Value <> ""  'Check if cell still has number
End Sub

--------------------------

Sub SAPExportCustom()
    Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
    Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
    Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
    Set session = SAPCon.Children(0) 'Get the first session (window) on that connection

    session.findById("wnd[0]").maximize
    session.findById("wnd[0]/usr/txt[35,5]").Text = j.Value 'Work center
...
    session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = j.Value & ".txt"
End Sub

我希望代码 co 回到 CustomList,将一个单元格向下移动到 A47 并复制它,然后 运行 SAPExportCustom 再次使用新变量。

你应该将参数传递给 SAPExportCustom,像这样:

Sub CustomList()
    Dim i As Long, j As String
    i = 46
    j = Cells(i, "A").Value
    While j <> ""
        SAPExportCustom (j)
        i = i + 1
        j = Cells(i, "A").Value
    Wend
End Sub

Sub SAPExportCustom(j As String)
    '...
    session.findById("wnd[0]/usr/txt[35,5]").Text = j
    '...
    session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = j & ".txt"
End Sub