Excel loop going through a certain number of times

I am working on some formulas in excel VBA and I am trying to do it after a certain number of times. Is it possible to do this in VBA, and if so, how should I do it?

My code looks like this:

Sub move()
    If ActiveCell.Offset(0, -1) = ActiveCell.Offset(1, -1) Then
        ActiveCell.Formula = "1"
        ActiveCell.Offset(1, 0).Select
    Else
        ActiveCell.Offset(1, 0).Select
    End If
End Sub

      

+3


source to share


1 answer


To cycle through a fixed number of times, use a cycle For...Next

.

In your case it will be like this (I did it for 10 loops, but you can change the number as you wish)



Sub move()

Dim i

For i = 1 To 10
    If ActiveCell.Offset(0, -1) = ActiveCell.Offset(1, -1) Then
        ActiveCell.Formula = "1"
    End If
    ActiveCell.Offset(1, 0).Select
Next i

End Sub

      

+4


source







All Articles