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?

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Latest member

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