Solved

I need help with a simple excel vba loop

Posted on 2013-05-15
3
240 Views
Last Modified: 2013-05-15
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

0
Comment
Question by:Ot_Gu
  • 2
3 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 39168784
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.
0
 
LVL 22

Accepted Solution

by:
rspahitz earned 500 total points
ID: 39168793
Maybe what you want is something more like this?

    For Each cell In ActiveSheet.Range("A1:A50")
        aktKolli = aktKolli + 1
      If aktKolli <= kolliAntal Then
           cell.Value = aktKolli
      Else
         Exit For
      End If
    Next cell

Open in new window

0
 

Author Closing Comment

by:Ot_Gu
ID: 39170408
That is what I wanted. Thanks you very much.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now