0

Can I somehow loop all this things, so that it will not take so much space and solve the program.

I have around 600, such type of MYarray elements. Suggest me something. how can i loop all these things.

I tried using a for loop and if statement within that loop .but was unsuccessful

    Myarray(1) = Sheets("DealComparison").Cells(2, 1)
    Myarray(2) = Sheets("DealComparison").Cells(3, 1)
    Myarray(3) = Sheets("DealComparison").Cells(4, 1)
    Myarray(4) = Sheets("DealComparison").Cells(5, 1)
    ..
    ..
    Myarray(600)=Sheets("DealComparison").Cells(601, 1)

so that the loop will automatically continue until there are no values.

Please help me solving this as i am new to vba.

I am not too sure about it,I think we need a for loop and a If loop with in FOR. Not too sure about it. Please help me do this Thanks

1
  • You could put the range into a 2-dimensional array in one line of code rather than looping. Commented Apr 28, 2015 at 13:33

1 Answer 1

2

You could use code similar to:

Sub dural()
    Dim I As Long
    Dim Myarray(1 To 4) As Variant

    For I = 1 To 4
        Myarray(I) = Sheets("DealComparison").Cells(I + 1, 1)
    Next I
End Sub

pick your own upper limit.

EDIT#1:

If you want the code to pick the upper limit then:

Sub dural()
    Dim I As Long, N As Long
    N = Sheets("DealComparison").Cells(Rows.Count, 1).End(xlUp).Row - 1
    Dim Myarray()
    ReDim Myarray(1 To N)

    For I = 1 To N
        Myarray(I) = Sheets("DealComparison").Cells(I + 1, 1)
    Next I
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

You just beat me to this. However I would use I = Ubound(Myarray)

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.