0

I have a dynamic array that I want to append values to. The number of values to be appended is not fixed

I was trying to do something like this:

Dim array() As Integer
ReDim Preserve array(UBound(array)+1)
bulkJob(UBound(array) + 1) = Me.ID

I get subscript out of range error at ReDim Preserve array(UBound(array)+1). Is there a way to do this?

3
  • I am surprised you can even run the code. I get syntax error on the Dim line. Array() is an intrinsic function. Use something else for your array name, like: aryInt. You ReDim the array but I don't see any code to populate the array. Commented Apr 17, 2019 at 2:02
  • 2
    UBound has no value so of course get out of bound error. Certainly can be done but not clear what you are trying to do. Where do the values come from? What is bulkjob? Commented Apr 17, 2019 at 2:18
  • You could also use a Collection if you want a dynamic array-like structure Commented Apr 17, 2019 at 2:23

1 Answer 1

1

Not quite clear what you are trying to do, but this could get you some ideas:

Public Function BuildJobs(Id As Integer)

    Static bulkJob()    As Integer
    Dim Upper           As Integer

    On Error Resume Next
    Upper = UBound(bulkJob) + 1
    On Error GoTo 0

    ReDim Preserve bulkJob(Upper)
    ' Fill in value.
    bulkJob(Upper) = Id

    ' Do something.
    Debug.Print UBound(bulkJob), bulkJob(Upper)

End Function

"Restart" the array like this:

ReDim bulkJob(0)
bulkJob(0) = 0
Sign up to request clarification or add additional context in comments.

Comments

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.