1
j = LBound(arrayTime)
Do Until j = UBound(arrayTime)
    j = j + 1
    b = b + 1
    cnc = b + r
    MsgBox cnc
        If cnc > 7 Then
            b = 0
            r = 0
            cnc = b + r
        End If
    numMins = Sheet5.Cells(cnc + 3, 2) - arrayTime(j)
    If numMins < 0 Then
        g = g + 1
        ReArrangeArray arrayTime, j
        'ReDim Preserve arrayTime(numrows - 1 + g)
        'arrayTime(numrows - 1 + g) = arrayTime(j)
        'MsgBox (arrayTime(numrows - 1 + g))
    Else
        Sheet5.Cells(cnc + 3, 2) = numMins
    End If
Loop

If the if statement is true I want to be able to put the array value at the end of the array and remove that value from its current spot. As the code is, it just adds it to the end and increases the size of the array from 12 to 13. How can I get the array to remain size 12 and still place the value at the end of the array and then remove it from its original position? I do not want to touch the array values in front. Just want to take that value and move it to the end. For instance array(1,2,3,4,5) If statement j on third loop. array(j)=3 end array should be array(1,2,4,5,3)

7
  • 1
    there should be a j=j+1 counter in the code Commented Jun 29, 2016 at 13:26
  • Google Bubblesort. You need to rearrange the array. Commented Jun 29, 2016 at 13:29
  • 1
    Possible duplicate of VBA array sort function? Commented Jun 29, 2016 at 13:34
  • re-arranging the array would be difficult for this application because I do not want it to move the array values in front of the value when the if statement is true. I hope that makes sense Commented Jun 29, 2016 at 13:57
  • Do you have code that works as you describe it? As @aggieman says, you're not incrementing your j counter - and where does variable c get defined? Commented Jun 29, 2016 at 14:41

1 Answer 1

1

You could use a helper Sub like this one:

Sub ReArrangeArray(inputArray as Variant, indexToSwap as long)
    Dim I As Long
    Dim tempVal As Variant

    If indexToSwap >= LBound(inputArray) And indexToSwap < UBound(inputArray)  Then
        tempVal = inputArray(indexToSwap)
        For I = indexToSwap To UBound(inputArray) - 1
            inputArray(i) = inputArray(i + 1)
        Next I
        InputArray(UBound(inputArray))  = tempVal 
    End If
End Sub

To be called by your main Sub as follows:

ReArrangeArray arrayTime, j
Sign up to request clarification or add additional context in comments.

4 Comments

its saying there is a byref argument mismatch with the j when i call the sub. How do I fix this?
Probably 'j' is declared as of "integer" type in your code while ReArrangeArray() accepts a "Long" type: either you change your Sub declaration of 'j' as being of "Long" type or change the 'ReArrangeArray' last parameter type to an "Integer" type
side note, why would anyone use integer when there are Long's ? it's like i'd use chr() when there is String...
I wouldn't say. Maybe "Integer" lasts from old languages. Or maybe books should stop frightening people with accurate choice of variable types not to waste memory as if we were still in the Commodore64 age...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.