0

I would like to create an array to store the name of different departments of the company. First of all, I make users to input the total amount of departments and then create a array, allowing users to key in the name of departements. But the compiler said the index of array is out of range.

Dim myarray As Variant
myarray = Array

deptnum = InputBox("Please enter the total amount of departments.")

For k = 0 To deptnum
x = InputBox("Please enter the name of department:"
x = myarray(k)
Next
4
  • sorry, is to "create" Commented Jul 20, 2020 at 8:41
  • 1
    Dont't use an array. Instead, use a COllection, or better still, a scripting.dictionary. Commented Jul 20, 2020 at 8:50
  • 1
    search redim preserve and ubound. That's what you need. Commented Jul 20, 2020 at 9:00
  • @cyboashu I will do that! thanks for your advice! Commented Jul 20, 2020 at 9:20

1 Answer 1

1

If you still use the array, you can use the redim array, then use array(index) to assign the name you have entered.

Sub Test()
    Dim departments() As String
    Dim x As String

    deptnum = InputBox("Please enter the total amount of departments.")
    If Not (IsNumeric(deptnum)) Then Exit Sub
    ReDim departments(deptnum - 1)

    For k = 0 To deptnum - 1
        x = InputBox("Please enter the name of department:")
        departments(k) = x
    Next
End Sub
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.