PDA

View Full Version : Limiting array size



stretch05
09-12-2011, 01:18 PM
I have an inventory problem I'm working on. Say we sell rope. We give 1000 ft to our branch. People buy quantities from our branch. If the branch is below a certain point, we need to send them another 1000 ft. When a reel gets down to a small enough point, I want to get rid of that reel.

In vba, I would increase the size of an array when another reel was needed. Now, I want to delete the smallest value from the array when the array size reaches a certain level (in this example, 4).

"Cutarray" has the size of rope available at the branch. I put the values into "temparray" if they are not the minimum. Then try to change the cutarray to what that temparray now equals, but it fails to work properly. It takes away one too many reels after it reaches the max amount of reels.

Here is the code I used.

Dim temparray As Variant
Dim Cutarray As Variant
Dim iMin As Double
Dim i
Dim newarray As Variant
Dim countit



If UBound(Cutarray) = 4 Then
countit = LBound(Cutarray)
countarray = 0

iMin = WorksheetFunction.Min(Cutarray)

ReDim temparray(0)

Do Until (countit - 1) = UBound(Cutarray)

If Cutarray(countit) = iMin Then
Else
If IsEmpty(temparray) Then

temparray = Array(Cutarray(countit))
Else
ReDim Preserve temparray(UBound(temparray) + 1)
temparray(UBound(temparray)) = Cutarray(countit)
End If
End If
countit = countit + 1
Loop

ReDim Cutarray(0, 1, 2, 3)
Cutarray = temparray

Else
End If

JWest
09-15-2011, 04:17 PM
Your issue appears to be with the



ReDim temparray(0)



Just remove it. When you assign an array size, it is no longer empty so it is skipping your line of code that reads



If IsEmpty(temparray) Then



The code will work as you intend without this line.