Consulting

Results 1 to 5 of 5

Thread: Loop

  1. #1
    VBAX Regular
    Joined
    Feb 2010
    Posts
    9
    Location

    Loop

    Hi,

    I am quite new to VBA coding and have some problems finding an easier way to write the following:

    Range("S5").Select
    ActiveCell.FormulaR1C1 = "=R[0]C[-13]"
    Range("S13").Select
    ActiveCell.FormulaR1C1 = "=R[-7]C[-13]"
    Range("S21").Select
    ActiveCell.FormulaR1C1 = "=R[-14]C[-13]"
    Range("S29").Select
    ActiveCell.FormulaR1C1 = "=R[-21]C[-13]"
    Range("S37").Select
    ActiveCell.FormulaR1C1 = "=R[-28]C[-13]"
    Range("S45").Select
    ActiveCell.FormulaR1C1 = "=R[-35]C[-13]"

    Any help is much appreciated.

    Best regards,

    Soren

  2. #2
    VBAX Expert shrivallabha's Avatar
    Joined
    Jan 2010
    Location
    Mumbai
    Posts
    750
    Location
    You can use for next loop. There will be some articles on this forum and loads of example. Looking at your current requirement

    [VBA]For i = 5 To 45 Step 8
    ActiveCell.FormulaR1C1 = "=R[0]C[-13]"
    Next i[/VBA]

    Hth,
    Regards,
    --------------------------------------------------------------------------------------------------------
    Shrivallabha
    --------------------------------------------------------------------------------------------------------
    Using Excel 2016 in Home / 2010 in Office
    --------------------------------------------------------------------------------------------------------

  3. #3
    VBAX Regular
    Joined
    Feb 2010
    Posts
    9
    Location
    Thanks a lot for your help...

    Regards,

    Soren

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Dim i As Long
    With Range("S5")

    For i = 0 To 40 Step 8
    .Offset(i, 0).FormulaR1C1 = "=R[-" & (i \ 8) * 7 & "]C[-13]"
    Next i
    End With
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Soren,

    Try:[VBA]Sub Demo()
    Dim i As Integer
    With Range("S5")
    For i = 0 To 5
    .Offset(i * 8, 0).FormulaR1C1 = "=R[" & -i * 7 & "]C[-13]"
    Next i
    End With
    End Sub[/VBA]
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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