visual basic formula help needed

  keith-236785 15:34 16 Dec 06

I am trying to write a simple Visual Basic program to help with my work.

my requirements are the mathematical formula needed (i never was good at maths).

the task is to take a set length (set by the user) and divide into it into the best fit of upto 3 sizes, ie an expensive piece of metal needs to be cut into different sized cuts to minimise the wastage.

ie, a 1025mm rod is to be cut up into 50, 60 and 70 mm pieces.

what i would like is a simple formula to do the calculation, the user (me) enters the overall width & the sizes required (could be 1,2 or 3) and then clicking a command button in visual basic would perform the calculation and output the result such as

10*50, 6*60, 2*70 = 1000 therefor a 25mm wastage, by putting a 70mm in place of a 50mm would only result in a 5mm wastage. currently this is done with a calculator and paper/pencil but it is time consuming and open to error ( i know i keep making them)

i have tried to write this as a basic program but i got lost quite quickly (again with the maths formula required).

one other consideration is that sometimes we may only need 5*50mm, so there would have to be a check so as to not overcut any particular size.

my main form asks the used for the
maximum rod size, then
size1 & quantity required, (2 seperate input boxes) then
size2 & quantity required, then
size3 & quantity required.

then it has a button for

Do Cuts (this is where i need to put the formula)

i may also consider using a combo box to allow the sizes/quantity to be picked with the mouse instead of trying them in but i am OK with that side of it.

please note that this is purely for my own benefit to help me do my work better (obviously it would save the company a small ammount of money but any requests for a commercial program to do this have met a brick wall) and also to help me learn visual basic.

i had also considered doing it via Excel and whilst i wont rule out this way, VB is the one i am wanting to learn.

for information, i am using windows XP & office 2000, but also have Visual Basic 6.0 to write a standalone prog

thanks in advance for any help that may come.

  stylehurst 17:05 16 Dec 06

Hi paperman
Like you, I dabble periodically with Visual Basic. When I get stuck I post a message on the VB forums
click here
So far I have always got an answer, fairly quickly, my interest is writing data capture programs for use in genealogy.

  keith-236785 20:40 16 Dec 06

many thanks for the link, i will check it out if no-one on here can help.

  Simsy 21:10 16 Dec 06

I can solve this for you, though I will give it some thought... (don't hold your breath!!)

But one thing springs to mind as an ommission, and that is the size/length/width, (or however you want to define it!), of the cutting kerf, the bit that's "wasted" by the cutting process.

I'm assumming that faily accurate accuracy is required, and so this could become significant...

To take a very simple example...

A rod is 1000mm and you want lengths of 500mm. If you assume the kerf is nil you can get 2 pieces out of the rod. But in reality it will not be nil, and it will be some finite, measurable distance... lets say it's 2mm...

If you cut one length of 500mm this means there is "only" 498mm left...

This is shorter than the 500mm required so, in reality, you need 10 1000mm rods for 10 500mm pieces... if you omit the kerf measurement you appear to be able to get 10 pieces from only 5 rods.

I would suggest that this error of 5 rods might manifest as a problem!

Only you can know how important the accuracy of the measurements is. As a learning exercise I would suggets that it's better to include the kerf measurement.

It either needs to be a constant built into the calculations, or, probably better, a variable inputted onto the form, with perhaps a default value that wouldn't, (normally), need changing.

Good luck,



  keith-236785 22:05 16 Dec 06

hmm, i see your point, however a deault is always allowed of 20mm so i would assume that this has already been taken into account, ie the true size would be 1020mm so maybe the kerf variation isnt going to be a problem, if it turns out to be then i can accomodate that by setting the default length to be


then the overall bar size would be reduced by 20mm before the calculation takes place. in effect giving a 40mm tolerance. this can be tweaked to suit when and if we get it working, if not then its back to the paper/pencil :-)

thanks for your help.

  keith-236785 22:29 16 Dec 06

one more point, i mentioned the maximum no of cut sizes was 3, that is true at this time but in the future it may need to be increased to maybe 6 or 7, so if it would be possible to ammend it easily enough then that would be great but if not then its not a worry.

my form is currently set to accept 3 sizes and the quantities required.

my first attempt at the formula was to check how many sizes required.

take 1*size1 and add 1*size2, total the size, deduct it from the overall length and divide the remainder by size3.

then it would have to check if the total of all the sizes was = to the overall length, if not then start again using 2*size1 and add 1*size2, total of them deducted from the overall length, if it still wasnt = then do again by adding another size1 and so on.

i got totally lost somewhere around 6....and thought there must be an easier way to do this.

it will probably turn out that it will never be = so the program would just always run size1 cuts.

once it has reached the maximum of size1 cuts it would then need to restart increasing size2 by 1 each time. i could build in an acceptable figure to use instead of = to overall length so it might eventually stop somewhere but then it might not reach the best fit calculation.

hope that makes sense, it sounds crazy and the basic code looked such a mess that i deleted it and tried in VB, but i am stuck there too.

thanks again

  Simsy 06:51 17 Dec 06

idea of "dolength=dolength-20" has failings as well, if we're going to be accurate...

It may be that there is no need to allow for kerf, (unlikely situation, but possible... length of original = 1000mm and length required = 1000mm for example). Also if there are 2 pieces, with only one cut, there is only need for a single kerf allowance.

It could be accomodated the way you suggest, but I suspect, (without having thought it all through), that it would make things easier too have the kerf size on the form, and thus in the basic arithmetic, rather than have it as you suggest, and need building into the eventual algorythym.

As usual, I'm open to correction!



  keith-236785 09:15 17 Dec 06

as i said before, im useless at im afraid you lost me there.

i think the allowance isnt going to be a major problem as i dont allow for it now and it all works ok (i guess the 20mm allowance is enough though i might be able to pinch a little of this in practice). the cut sizes will be in the range from 52mm to 193mm, there are instances where we cut larger sizes but that sort of takes care of itself, only 4 or 5 cuts i can work out quick enough on paper without much error.

please dont spend too much time and effort on this, it is after all only to make my work a little easier.

thanks anyway.

This thread is now locked and can not be replied to.

Nintendo Switch review: Hands-on with the intuitive modular console and its disappointing games…

1995-2015: How technology has changed the world in 20 years

This abstract video touches on division in our technologic world

Best alternatives to iTunes for Mac | Best music players for macOS: Free your music from the…