How I can add a for loop to run in a script of SAP GUI and Excel macro?

I have an Excel macro that already runs in SAP GUI. Two parts of the script need to repeat the same step a certain number of times according to the components that exist in the order. How can I add two for loops that read from Excel the number of times the activity should be repeated (ComponentsQty)? I have already declared the variable "ComponentsQty" which will read the number of times the loop should run.

Sub Orders()
x = x + 1
conteo = Cells(x, 1).Value
While conteo <> Empty
x = x + 1
conteo = Cells(x, 1).Value
Wend

Dim App, Connection, session As Object
On Error GoTo ErrorHandler
Set SapGuiAuto = GetObject("SAPGUI")
Set App = SapGuiAuto.GetScriptingEngine
Set Connection = App.Children(0)
Set session = Connection.Children(0)
For n = 2 To x - 1
e = 0
Order = Cells(n, 1).Value
OrderReason = Cells(n, 2).Value
ComponentsQty = Cells(n, 3).Value

session.findById("wnd[0]").resizeWorkingPane 128, 37, False
session.findById("wnd[0]/tbar[0]/okcd").Text = "VA02"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtVBAK-VBELN").Text = Order
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT01/ssubSUBSCREEN_BODY:SAPMV45A:4400/ssubHEADER_FRAME:SAPMV45A:4440/radRV45A-RB_AAUART1").Select
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT01/ssubSUBSCREEN_BODY:SAPMV45A:4400/ssubHEADER_FRAME:SAPMV45A:4440/cmbVBAK-LIFSK").Key = " "
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT01/ssubSUBSCREEN_BODY:SAPMV45A:4400/ssubHEADER_FRAME:SAPMV45A:4440/cmbVBAK-LIFSK").SetFocus
session.findById("wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/btnBT_HEAD").press
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]/usr/btnSPOP-VAROPTION1").press
Application.Wait (Now + TimeValue("0:00:02"))

session.findById("wnd[1]").sendVKey 0

'This step is going to be repeated according to a value in excel

session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0
session.findById("wnd[1]").sendVKey 0

session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT01/ssubSUBSCREEN_BODY:SAPMV45A:4301/cmbVBAK-AUGRU").Key = OrderReason
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT01/ssubSUBSCREEN_BODY:SAPMV45A:4301/cmbVBAK-AUGRU").SetFocus
Application.Wait (Now + TimeValue("0:00:02"))

session.findById("wnd[0]").sendVKey 0

'This step is going to be repeated according to a value in excel

session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/tbar[0]/btn[3]").press

Application.Wait (Now + TimeValue("0:00:02"))

Cells(n, 1).Style = "Good"
If e = 1 Then
Cells(n, 1).Style = "Bad"
End If

'Termina
Next n

Exit Sub
ErrorHandler:  ' Error-handling routine.
Select Case Err.Number   ' Evaluate error number.
Case 619   ' Run-time error '619'
e = 1 '***** Variable de control = 1 cuando aparezca el error 619 *****
Case Else
' Insert code to handle other situations here...
End Select
Resume Next  ' Resume execution at same line
' that caused the error.

End Sub

Upvotes: -1

Views: 41

Answers (1)

Tim Williams
Tim Williams

Reputation: 166081

For example:

Dim i As Long, ComponentsQty As Long
'...
ComponentsQty = Cells(n, 3).Value
'...
For i = 1 to ComponentsQty
    session.findById("wnd\[1\]").sendVKey 0
Next i
'...

Upvotes: 0

Related Questions