简体   繁体   English

如何循环遍历 Excel 行

[英]How to loop through Excel rows

I'm trying to build an Excel VBA script that runs through an Excel sheet and extracts reports from a SAP system and puts them in a specific folder.我正在尝试构建一个 Excel VBA 脚本,该脚本通过 Excel 工作表运行并从 SAP 系统中提取报告并将它们放在特定文件夹中。 The parameters start at A10 and B10 and these are put in the SAP screen fields where the report is created and downloaded in the folder on my desktop.参数从 A10 和 B10 开始,它们被放在 SAP 屏幕字段中,在该字段中创建报告并将其下载到我桌面上的文件夹中。 Please see here below an example of the Excel sheet and a screenshot of the SAP screen and the code I'm using.请在下面查看 Excel 工作表的示例和 SAP 屏幕的屏幕截图以及我正在使用的代码。

The script works fine for the first parameters A10 and B10 (it extracts a report from the SAP system and puts it in the desired folder) however I would like to loop the script to extract reports automatically for the other parameters below A10 and B10 and beyond (Loop Until ActiveCell.Value = "")该脚本适用于第一个参数 A10 和 B10(它从 SAP 系统中提取报告并将其放在所需的文件夹中)但是我想循环脚本以自动提取 A10 和 B10 及以后的其他参数的报告(Loop Until ActiveCell.Value = "")

Can someone help me with this?有人可以帮我弄这个吗? Where and how do I put the loop?我在哪里以及如何放置循环?

excel 带参数

在此处输入图片说明

The code:编码:

'Declaring variables for sub procedures

Option Explicit
Public SapGuiAuto, WScript, msgcol
Public objGui  As GuiApplication
Public objConn As GuiConnection
Public session As GuiSession

'Creating sub procedure

Sub SAPCustomerReport()

'Pointing object variables to SAP session

Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
Set objConn = objGui.Children(0)
Set session = objConn.Children(0)
Dim Vendor As String
Dim CoCo As String
Dim FolderPath As String
Dim SAPOutputLayout As String

Vendor = ActiveWorkbook.ActiveSheet.Range("A10")
CoCo = ActiveWorkbook.ActiveSheet.Range("B10")
FolderPath = ActiveWorkbook.ActiveSheet.Range("B3")
SAPOutputLayout = ActiveWorkbook.ActiveSheet.Range("B4")

'Recorded SAP Script here

session.FindById("wnd[0]").Maximize
session.FindById("wnd[0]/tbar[0]/okcd").Text = "/nFBL1N"
session.FindById("wnd[0]").SendVKey 0
session.FindById("wnd[0]/usr/chkX_SHBV").Selected = True
session.FindById("wnd[0]/usr/chkX_MERK").Selected = True
session.FindById("wnd[0]/usr/chkX_PARK").Selected = True
session.FindById("wnd[0]/usr/ctxtKD_LIFNR-LOW").Text = Vendor
session.FindById("wnd[0]/usr/ctxtKD_BUKRS-LOW").Text = CoCo
session.FindById("wnd[0]/usr/ctxtPA_VARI").Text = SAPOutputLayout
session.FindById("wnd[0]/usr/ctxtPA_VARI").SetFocus
session.FindById("wnd[0]/usr/ctxtPA_VARI").CaretPosition = 12
session.FindById("wnd[0]/tbar[1]/btn[8]").Press
session.FindById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select
session.FindById("wnd[1]/usr/ctxtDY_PATH").Text = FolderPath
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").Text = Vendor & CoCo & ".XLSX"
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").CaretPosition = 4
session.FindById("wnd[1]/tbar[0]/btn[11]").Press

MsgBox "Script Completed."

End Sub

A solution could look like this:解决方案可能如下所示:

Sub SAPCustomerReport()
...
Recorded SAP Script here
i = 10
do
 session.FindById("wnd[0]").Maximize
 session.FindById("wnd[0]/tbar[0]/okcd").Text = "/nFBL1N"
 ...
 session.FindById("wnd[1]/tbar[0]/btn[11]").Press
 i = i + 1
 Vendor = ActiveWorkbook.ActiveSheet.Range("A" & cstr(i))
 CoCo = ActiveWorkbook.ActiveSheet.Range("B" & cstr(i))
loop until Vendor = ""

MsgBox "Script Completed."

End Sub

Regards, ScriptMan问候, ScriptMan

You should split the code in a routine that iterates over the configuration and one that takes as input vendor and CoCo and creates the report.您应该将代码拆分为迭代配置的例程,以及将供应商和 CoCo 作为输入并创建报告的例程。

Furthermore I suggest to format your configuration (A9:B12) as a table (Ribbon: Insert > table) then you can use the listobject in VBA - which is much easier to handle regarding ranges etc. I named the table "tblSAPReports".此外,我建议将您的配置 (A9:B12) 格式化为表格(功能区:插入 > 表格),然后您可以在 VBA 中使用列表对象 - 这在范围等方面更容易处理。我将表格命名为“tblSAPReports”。

Option Explicit

Sub createAllReports()
Dim lo As ListObject
Set lo = ThisWorkbook.Worksheets("Table1").ListObjects("tblSAPReports") 'adjust to your needs

Dim lr As ListRow
Dim Vendor As String, CoCo As String
For Each lr In lo.ListRows
    Vendor = lr.Range(1, 1): CoCo = lr.Range(1, 2)
    SAPCustomerReport Vendor, CoCo
Next

End Sub




Public Sub SAPCustomerReport(Vendor As String, CoCo As String)

'your code
'but remove Vendor and CoCo variable and the setting of the variables

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM