Issue with Excel VBA using multiple For and Next

bthumble

Board Regular
Joined
Dec 18, 2007
Messages
239
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!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
You should post code within code tags (vba button on posting toolbar) to maintain indentation and readability. Trying to follow nested blocks with code posted like that is not something I like to do, and TBH I'm usually too lazy to fix it for everyone :) It comes about after a few thousand responses.

On the surface it seems you should not have 2 nested loops inside the outer one. The first loop runs then the second begins, then the third begins. The 3rd runs 3 times in total. When complete the second runs the next loop, which runs the 3rd 3x. Then the second loop runs the 3rd time which runs the 3rd loop 3x. Then let's go back to the outer loop and run that the second time, which....

You could have seen this by stepping through your code - troubleshooting 101. I'm curious to know how many loops in total. I figure maybe 25 or 27. Now I'm curious.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top