Loop with (changing) variables


New Member
Aug 1, 2017

I have to admit I am horrible with VBA or programming in general. I often can read the simple stuff but making it is a lot harder. For this one I have searched for solutions, but I cannot find it. Either it seems it is not the solution for me (might be too complicated looking for me so I don't recognize it as the solution) or because I am no good with it, I have trouble searching for the right terms.

I will first explain what we have, then what it currently does and what I need it to be.

We create bill of materials in SAP with excel VBA scripting. The excel has in Column B the BOM code, column C the component and in column D the amount. Column A would say if the line is a header (h) or component (c).
Based on this we have a script that creates a bom with header in column B, and puts in components until the next line starts with 'h' in col A. Then it goes on the next BoM.

Now we use also offsets, and this used to be the same value. So for this I was able to build in a loop before the script would go to the next BoM.

Sub CreateOffset(oSession)

oSession.findById("wnd[0]/tbar[1]/btn[27]").press 'select all
oSession.findById("wnd[0]/tbar[1]/btn[7]").press 'open items

'loop this part.

'If clause that ends the loop.
If oSession.findById("wnd[0]/sbar").messagetype = "S" Then
Exit Do
End If

'Lead time can be changed with the values in the 3 grey lines below
oSession.findById("wnd[0]/usr/tabsTS_ITEM/tabpPHPT/ssubSUBPAGE:SAPLCSDI:0830/txtRC29P-NLFZT").Text = "30"
oSession.findById("wnd[0]/usr/tabsTS_ITEM/tabpPHPT/ssubSUBPAGE:SAPLCSDI:0830/txtRC29P-NLFZV").Text = "1"
oSession.findById("wnd[0]/usr/tabsTS_ITEM/tabpPHPT/ssubSUBPAGE:SAPLCSDI:0830/ctxtRC29P-NLFMV").Text = "DAY"
oSession.findById("wnd[0]/usr/tabsTS_ITEM/tabpPHPT/ssubSUBPAGE:SAPLCSDI:0830/ctxtRC29P-NLFMV").caretPosition = 3
oSession.findById("wnd[0]/tbar[1]/btn[18]").press 'Next item



End Sub

This all works quite nice. This sub selects in SAP all items, goes into the item details, fills in the offset values and hits a button to go to the next item. This will continue until the statusbar on the bottom would return a specific message and then the Sub would stop and the script would continue normally again. I didn't have to count the number of components this way (this is not always the same number) since that messagetype S will only be shown in the statusbar if you try to go to the next item but there is no next item anymore.

The thing now is this offset of '30' in the loop above can be a variable. But since this sub does not count but just hits next all the time this wont work. I've been looking in all kinds of stuff but I am not able to redo this in a different way. What would be the best way to continue with this? Preferably I would add these variables in column D and E etc and then have it take those values but creating this loop was already quite fancy for me. Anyone any idea how to solve this?

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Watch MrExcel Video

Forum statistics

Latest member