Using a for statement instead of a for Each

  • I am trying to remember how to do this.


    I want to create a column of numbers, 10 rows deep, ranging from 1 to 10.


    I have the below formula using a for each statement. How would I modify this just using a straight For loop:


    Sub RangeofNumbers()
    x = 1
    For Each cell In Range("A:A10")
    cell.Value = x
    x = x + 1
    Next
    End Sub


    I haven't used VBA in a while so it is somewhat fuzzy at the moment.


    Thanks

  • Thanks Derk,


    Now, if I wanted to have 5 columns starting from 1 and ending with 50 (column 5), how would the code be modified?

  • Hi bc,


    Something like this:

    HTH

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!