0

I need to fill an array with a fixed pattern number sequence such as every x number of an element needs to add 1.

I tried to use for to step, as in:

Option Explicit
Sub Test_Array()
    Dim Myarray(1 To 10) As Double
    Dim Element As Double
    Dim i As Long
    i = 1
    For Element = LBound(Myarray) To UBound(Myarray) Step 2
        Myarray(Element) = i
        i = i + 1
    Next Element    
End Sub 

The result is as follows:

Myarray=[1,0,2,0,3,0,4,0,5,0]

But between each step needs to be populated with the previous element as the next example:

Myarray=[1,1,2,2,3,3,4,4,5,5]

I don't know if for to step is the best choise.

0

1 Answer 1

3

You could do this:

Const STEP_SIZE As Long = 2
Dim Myarray(1 To 10) As Double
Dim Element As Long

For Element = LBound(Myarray) To UBound(Myarray)
    Myarray(Element) = Application.Ceiling(Element / STEP_SIZE, 1)
Next Element
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.