Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select objects from boxes using IF or

Status
Not open for further replies.

elbehh

Programmer
Oct 25, 2006
5
Hello

It was given array "Obj"[/color blue] (number of objects what I must extract from ...) array "Box"[/color blue] (number of objects in this box).
I must select Objects in the following conditions ("Rez" the rezult how it looks like)


| Obj | Box | dif | Rez
---------------------------------------------------
1 | 4 | 7 | 3 | 4
2 | 3 | 9 | 6 | 3
3 | 2 | 7 | 5 | 2
4 | 2 [/color red]| 6 | 4 | 3[/color red]
5 | 3 | 2 | -1 | 2[/color red]
6 | 1 | 3 | 2 | 1
7 | 2 | 2 | 0 | 2
8 | 1 | 1 | 0 | 1
---------------------------------------------------
Sum | 18 | 37 | | 18
[/color blue]
The problem
I have 8 boxes … 7 objects in the first, 9 objects in the second, 6 in the third ... and so on...
I will start from the end, to extract objects - from Box8 to extract 1 object, from Box7 - 2 objects ...
But from the Box5[/color red] I cannot extract 3[/color red] objects because I have only 2 !!![/color red]
That means from Box4[/color red] I have to extract 2[/color red] objects, plus the difference from Box5, 1[/color red] object, total 3[/color red] objects

Details
I must extract around half of the number of objects.
Usually, the distribution of the objects is - less number of objects in Box8 and it is growing up to Box1
I mean - I can not have 36 objects in Box8 and nothing in the others.
The same it' s the distribution of objects what I have to extract - it is growing up from the end to the beginning.
To extract objects starting with Box1, I think it’s not so good because on the other way round I’m sure that I have ob-jects - the number of objects is growing up
I tried to make it simply but I finished with big-big algorithm.[/color red]
I show it bellow, only a portion ...


IF A8-B8 < 0 THEN
IF A7-B7-A8+B8 < 0 THEN
IF A6-B6-A7+B7+A8-B8 < 0 THEN
IF A5-B5-A6+B6+A7-B7-A8+B8 < 0 THEN
IF A4-B4-A5+B5+A6-B6-A7+B7+A8-B8 < 0 THEN
IF A3-B3-A4+B4+A5-B5-A6+B6+A7-B7-A8+B8 < 0 THEN
IF A2-B2-A3+B3+A4-B4-A5+B5+A6-B6-A7+B7+A8-B8 < 0 THEN
IF A1-B1-A2+B2+A3-B3-A4+B4+A5-B5-A6+B6+A7-B7-A8+B8 < 0 THEN
TextBox ("it is imposible")
ELSE
C1=A1
………….
ELSE
C2=A2
………….
ELSE
C3=A3
………….
ELSE
C4=A4
………….
ELSE
C5=A5
ELSE
C6=A6
ELSE
C7=A7
ELSE
C8=A8
IF A7-B7-A8+B8 < 0 THEN
IF A6-B6-A7+B7+A8-B8 < 0 THEN
IF A5-B5-A6+B6+A7-B7-A8+B8 < 0 THEN
IF A4-B4-A5+B5+A6-B6-A7+B7+A8-B8 < 0 THEN
IF A3-B3-A4+B4+A5-B5-A6+B6+A7-B7-A8+B8 < 0 THEN
IF A2-B2-A3+B3+A4-B4-A5+B5+A6-B6-A7+B7+A8-B8 < 0 THEN
IF A1-B1-A2+B2+A3-B3-A4+B4+A5-B5-A6+B6+A7-B7-A8+B8 < 0 THEN
TextBox ("it is imposible")
ELSE
C1=A1
ELSE
C2=A2
ELSE
C3=A3
[/color blue]

Thank you
 
i hope this is not an IQ test cause i don't understand a word of it. i would like to help cause of 0 replies but I'm sorry i can't. Do you have a simpler version?
 

I know ... I was confusing ... but, I will try again, I hope much clear this time !!

I have eight boxes box1 box2 box3 box4 box5 box6 box7 box8
every box has inside 1 3 3 2 6 7 9 7 objects
I want to extract 1 2 1 3 2 2 3 4 objects from every box
starting from the left to the right ... in this >--->--->---->---> direction


box1 I have 1 obj, I must extract 1 obj
box2 I have 3 obj , I must extract 2 obj, no problem
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
box4[/color red] I have 2 obj but I must extract 3 obj, that means it remains 1 obj to extract from the next box
box5[/color blue] I have 6 obj and I must extract 2obj+1obj remained=3obj
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
box8 I have 7 obj, I must extract 4 obj

In the end, the array of objects extracted will be 1,2,1,2[/color red],3[/color blue],2,3,4
not the same like the array what I must extract 1,2,1,3,2,2,3,4


I need help for an algorithm in VBA what is working with two array "Box" and "Obj" and the result to be other array. "Extracted"

Thank you for your time and patience !
 
A starting point:
Code:
Dim arrObj(), arr2Ex(), i As Integer
arrObj = Array(1, 3, 3, 2, 6, 7, 9, 7)
arr2Ex = Array(1, 2, 1, 3, 2, 2, 3, 4)
For i = LBound(arr2Ex) To UBound(arr2Ex) - 1
  If arr2Ex(i) > arrObj(i) Then
    arr2Ex(i + 1) = arr2Ex(i + 1) + arr2Ex(i) - arrObj(i)
    arr2Ex(i) = arrObj(i)
  End If
Next
If arr2Ex(i) > arrObj(i) Then
  MsgBox "impossible"
Else
  MsgBox Join(arr2Ex, ",")
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

I tried in my program, everything it seems to be Ok !!
Thank you for your help !
 
I wish the heck I knew what this was about. I am trying to get my head around "objects" being inside "boxes". I keep thinking this means object names inside an array.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top