If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > Stumped - New to a "Loop"

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-20-08, 17:37
Bob.Carter.17 Bob.Carter.17 is offline
Registered User
 
Join Date: Dec 2005
Posts: 111
Stumped - New to a "Loop"

I know this has to be simple to most, but Loops are new to me.

I have some lines of code that need to run a number of times. The number of times it needs to run is the total of how many territories, regions and districts we have. I have that number calculated and populating cell AA1, currently as 117 total. So this code needs to run 117 times. I have tried:

counter = 0
MyNum = 1
Do
MyNum = MyNum + 1
counter = counter + 1

(my code inserted here)

Loop Until MyNum = AA1

It runs, but it won't stop, so it is not seeing the total of 117 in cell AA1. What am I missing?
Reply With Quote
  #2 (permalink)  
Old 10-21-08, 03:35
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
MyNum is an integer, and you're looping until it equals AA1 (not an integer!)...

Do you think this condition will ever be met?
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 10-21-08, 12:06
Bob.Carter.17 Bob.Carter.17 is offline
Registered User
 
Join Date: Dec 2005
Posts: 111
Probably not. I have since changed it to the below and I can get it to work fine, except I still cannot get the line to see I want to use the value in cell AA1.

counter = 0
MyNum = 0
Do
MyNum = MyNum + 1
counter = counter + 1
ActiveCell.Range("A1:A1").Select
Cells.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Selection.delete Shift:=xlToLeft
ActiveCell.Offset(rowOffset:=-1, columnOffset:=0).Activate
Selection.Copy
ActiveCell.Offset(rowOffset:=1, columnOffset:=0).Activate
ActiveSheet.Paste
Selection.End(xlUp).Select
Loop Until MyNum = 117

When I try to change the Loop Until MyNum = from 117 to AA1. I don't understand how to make MyNum reflect the value in cell AA1 I guess. Or should I be using a different method. I found this in the help file as using a Loop until condition becomes true method.
Reply With Quote
  #4 (permalink)  
Old 10-21-08, 19:04
shades shades is offline
Registered User
 
Join Date: Oct 2003
Posts: 1,091
Add a conditional line to check the value (MyNum = value of AA1), before it continues on the loop, something along this line assuming that cell AA1 is the reference: (not tested)

If MyNum > Cells(1,27).Value Then Exit
__________________
old, slow, and confused
but at least I'm inconsistent!

Rich
(retired Excel 2003 user, 3/28/2008)

How to ask a question on forums

Last edited by shades; 10-21-08 at 19:09.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On