Consulting

Results 1 to 6 of 6

Thread: Solved: Loop with Go to steps?

  1. #1

    Solved: Loop with Go to steps?

    Hi, Can someone shed me some light on how to write this?

    I have a few instructions
    -Write 15 in cell A1
    -Write 25 in cell A2
    -Write 99 in cell A3
    etc.

    If a loop runs, I want to loop to execute the first line, then increment the cell by one, and then execute the second line.

    Thanks all

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Option Explicit

    Sub test()
    Dim arr(), a
    Dim i As Long

    arr = Array(15, 25, 99)
    For Each a In arr
    i = i + 1
    Range("A" & i) = a
    Next
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    increment the cell by one,
    Increment each of the the cells's VALUEs by 1, or go to the next cell in your list?

    In other words, do you want to end up with

    A1 = 15
    A2 = 25
    A3 = 99

    or

    A1 = 16
    A2 = 26
    A3 = 100


    or something else?

    Paul

  4. #4
    There are probably a million ways to do this

    I personally prefer using this Format to loop through cells

    R=1

    Do
    Cells (R,1) = XXXXX ' Cells (Row, Column)

    R=R+1
    Loop until R = YYY

    In your specific case you need to fill up an array with your results
    If it's only 3 values you can use a Case Select or use that array thing like mdmackillp suggested up there (That's a new trick for me!)

    If there are many values, you can step through and transfer them over one by one or store them all up in an array. Lots of different ways to do it. It all depends on your style.

    Good Luck

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Of course if you don't need to do other things that require a loop:
    [VBA]Sub t()
    Dim a() As Variant
    a() = [{15,25,99}]
    Range("A1").Resize(UBound(a)).Value2 = WorksheetFunction.Transpose(a)
    End Sub[/VBA]

  6. #6
    great. thanks all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •