Link to home
Start Free TrialLog in
Avatar of Ot_Gu
Ot_Gu

asked on

I need help with a simple excel vba loop

Hello All,

Why doesn't this stop in cell A15?
Any help appreciated!

Thanks.
Sub StopatFifteen()
Dim aktKolli As Long
Dim kolliAntal As Long

kolliAntal = 15
aktKolli = 0

Do While aktKolli < kolliAntal
    
    For Each cell In ActiveSheet.Range("A1:A50")
        aktKolli = aktKolli + 1
        cell.Value = aktKolli
    Next cell
Loop
    
End Sub

Open in new window

Avatar of rspahitz
rspahitz
Flag of United States of America image

Why doesn't it stop?
Because your loop condition is outside your For loop, so the for loop does all 50 cells before checking for the next value.
ASKER CERTIFIED SOLUTION
Avatar of rspahitz
rspahitz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ot_Gu
Ot_Gu

ASKER

That is what I wanted. Thanks you very much.