My excel macro that runs reports in SAP has multiple for and next statements. The macro keeps running even though it ran the reports correctly. Am I doing something incorrect?
Sub GR55_Macro()
Dim dts As String
Dim i As Integer
Dim j As Integer
Dim k As Integer
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
Set objConn = objGui.Children(0)
Set session = objConn.Children(0)
Set ws = ThisWorkbook.Sheets("Sheet1")
Set WScript = CreateObject("WScript.Shell")
dts = Format(Now, "mm-dd-yyyy- hh-mm-ss")
For i = 4 To 6
For j = 4 To 6
For k = 4 To 6
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nGR55"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRGRWJ-JOB").Text = "Z123"
session.findById("wnd[0]/usr/ctxtRGRWJ-JOB").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/mbar/menu[3]/menu[3]").Select
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/txt$ZEJER3").Text = ws.Range("D" & i).Value
session.findById("wnd[0]/usr/txt$ZPERDES").Text = ws.Range("E" & j).Value
session.findById("wnd[0]/usr/txt$ZPERHAS").Text = ws.Range("F" & k).Value
session.findById("wnd[0]/usr/txt$ZPERHAS").SetFocus
session.findById("wnd[0]/usr/txt$ZPERHAS").caretPosition = 3
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/mbar/menu[0]/menu[3]").Select
session.findById("wnd[1]/usr/radLGRWO-X_EXPONLY1").Select
session.findById("wnd[1]/usr/ctxtLGRWO-OUT_FILE").Text = ws.Range("filename") & " Z123 " & ws.Range("D" & i) & " " & ws.Range("E" & j) & " " & ws.Range("F" & k) & " " & dts & ".dat"
session.findById("wnd[1]/usr/radLGRWO-X_EXPONLY1").SetFocus
session.findById("wnd[1]/tbar[0]/btn[0]").press
Next k
Next j
Next i
End Sub
Thanks for your help!
Sub GR55_Macro()
Dim dts As String
Dim i As Integer
Dim j As Integer
Dim k As Integer
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
Set objConn = objGui.Children(0)
Set session = objConn.Children(0)
Set ws = ThisWorkbook.Sheets("Sheet1")
Set WScript = CreateObject("WScript.Shell")
dts = Format(Now, "mm-dd-yyyy- hh-mm-ss")
For i = 4 To 6
For j = 4 To 6
For k = 4 To 6
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nGR55"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRGRWJ-JOB").Text = "Z123"
session.findById("wnd[0]/usr/ctxtRGRWJ-JOB").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/mbar/menu[3]/menu[3]").Select
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/txt$ZEJER3").Text = ws.Range("D" & i).Value
session.findById("wnd[0]/usr/txt$ZPERDES").Text = ws.Range("E" & j).Value
session.findById("wnd[0]/usr/txt$ZPERHAS").Text = ws.Range("F" & k).Value
session.findById("wnd[0]/usr/txt$ZPERHAS").SetFocus
session.findById("wnd[0]/usr/txt$ZPERHAS").caretPosition = 3
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/mbar/menu[0]/menu[3]").Select
session.findById("wnd[1]/usr/radLGRWO-X_EXPONLY1").Select
session.findById("wnd[1]/usr/ctxtLGRWO-OUT_FILE").Text = ws.Range("filename") & " Z123 " & ws.Range("D" & i) & " " & ws.Range("E" & j) & " " & ws.Range("F" & k) & " " & dts & ".dat"
session.findById("wnd[1]/usr/radLGRWO-X_EXPONLY1").SetFocus
session.findById("wnd[1]/tbar[0]/btn[0]").press
Next k
Next j
Next i
End Sub
Thanks for your help!