Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to fill an array with numbers 1-50, and I currently have the code:

Dim numberSet(49)
For x = 1 To 50
numberSet(x - 1) = x
Next x

The challenge is to do it in the least amount of lines possible. This part is bugging me because it seems like i shouldn't be using 4 lines for something so basic.

Any thoughts from you guys? I want to avoid doing something like = {1,2,3,4,5...50} if I can. Thanks!

share|improve this question

2 Answers

up vote 0 down vote accepted

In one line:

Dim numberSet(49): For x = 1 To 50: numberSet(x - 1) = x: Next x
share|improve this answer
Thanks! Is there any way to do something like ={1-50} ? – hellohellosharp 2 days ago
Not that I know of in VBA. – Robert Harvey 2 days ago

One line (but it creates a 1-based array...)

Sub TT()

    Dim arr

    arr = Application.Transpose([=ROW(A1:A50)])

    Debug.Print UBound(arr)
    Debug.Print arr(1)
    Debug.Print arr(13)

End Sub

...and if you turn off Option Explicit you can skip the declaration. But don't do that ;-)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.