Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

I need help with a simple excel vba loop

Posted on 2013-05-15
3
Medium Priority
?
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Make the most of your online learning experience.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

704 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