473,395 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

code stuck in endless loop

175 100+
Expand|Select|Wrap|Line Numbers
  1.  
  2. Sub Macro1()
  3.  
  4.  
  5. Dim num As Integer
  6. Dim scan As Integer
  7. Dim start As Integer
  8. Dim total As Integer
  9.  
  10. start = 1
  11.  
  12. While Range("A" & start) <> ""
  13.     start = start + 1
  14. Wend
  15.  
  16. total = start - 1
  17. num = 1
  18.  
  19. For num = 1 To total
  20.     For scan = (num + 1) To start
  21.         While Range("A" & scan) = Range("A" & num)
  22.             If Range("A" & scan) = Range("A" & num) Then
  23.                 Range("A" & scan).Delete shift:=xlUp
  24.             End If
  25.         Wend
  26.     Next
  27. Next
  28.  
  29.  
  30. End Sub
  31.  
  32.  
this is VBA code for excel. It looks at column A, and counts up the number of non empty cells. Then, it looks to see if there are any repeats and deletes them out.

The problem I'm having is that it goes into an endless loop. I've stepped through the code, and when it reaches "End Sub", the program starts again from the beginning of the code. I have no idea why this is happening. Any help is appreciated.
Feb 1 '07 #1
5 4892
Killer42
8,435 Expert 8TB
Hm... are you sure you haven't set up an event procedure to call this macro when a cell is changed, or something? If that were the case, then having your macro make a change to the sheet could cause it to be invoked again.
Feb 1 '07 #2
manontheedge
175 100+
yeah, that code is literally all i have in the excel file ( i even opened a new file just in case and pasted the code ), except for some random data in column A. I went over it for quite a while and couldn't find anything to cause this loop.

i'm fairly new to VBA...is there any way to force the code to exit?
Feb 1 '07 #3
Killer42
8,435 Expert 8TB
yeah, that code is literally all i have in the excel file ( i even opened a new file just in case and pasted the code ), except for some random data in column A. I went over it for quite a while and couldn't find anything to cause this loop.

i'm fairly new to VBA...is there any way to force the code to exit?
I suppose you could try an Exit Sub or End statement. From the sound of it though, you are already exiting the sub normally at the end, and it is being re-invoked for some reason.

I suppose you might fall back on "standard" debugging techniques and try to narrow the focus. I'd suggest one of the following two methods:
  1. Start commenting out parts of the code until it stops going wrong, or
  2. Create a new macro, add in your code bit by bit and see at what point it starts going wrong.
You should be able to pin down what is causing the loop, even if that doesn't tell us why.

P.S. How are you invoking the macro? Just running it from the macros menu?
Feb 1 '07 #4
manontheedge
175 100+
I tried commenting out one part at a time, and when I was trying to decide what to comment out I saw the problem. There is a while loop in the two FOR loops...the while loop continues while the previous cell is the same. The problem comes when they are both empty. The condition is always true. So I stuck in an IF statement before the while loop, and it works fine now. Thanks for the help man. Guess I just needed to break it down. Frustration got to me.
Feb 1 '07 #5
Killer42
8,435 Expert 8TB
I tried commenting out one part at a time, and when I was trying to decide what to comment out I saw the problem. There is a while loop in the two FOR loops...the while loop continues while the previous cell is the same. The problem comes when they are both empty. The condition is always true. So I stuck in an IF statement before the while loop, and it works fine now. Thanks for the help man. Guess I just needed to break it down. Frustration got to me.
Yeah, it can do that.

I find that when it comes to debugging, the important thing is breaking it down to the simplest level to identify the problem. Fixing it is generally the easy part.

Congratulations! :)


P.S. I still don't get why it would have been starting over at the beginning of the macro, though.
Feb 1 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: John F. | last post by:
Hi all, I'm searching fulltime for days now, and I don't find the solution, so I'm thinking this is more a bug :( If i use following code in form&subforms:
24
by: Tweaxor | last post by:
This has been puzzling me all this week. This is actually a homework assignment from last semesmter. But the teacher wouldn't tell us why certain things didn't work, but it didn't just work. My...
13
by: Bev in TX | last post by:
We are using Visual Studio .NET 2003. When using that compiler, the following example code goes into an endless loop in the "while" loop when the /Og optimization option is used: #include...
5
by: Mike D | last post by:
Attached is my code. Which I know there is a better way of writing however this is what I came up with and it works until the value is null or not = to the <> value. 'Do While rdrSQL.Read() ...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
7
by: tlyczko | last post by:
I am writing code to get a subset of table records, but it is not looping properly through the fields, it keeps getting stuck in some sort of infinite loop and I have to be able to walk the whole...
8
by: Steve Edwards | last post by:
Hi, While iterating through a multimap, I need to replace elements that meet certain conditions with a new element. There doesm't seem to be a replace() function for multimaps, so I'm inserting...
6
by: RJ | last post by:
I'm trying to teach myself Python (probably running into the old dog new tricks issue) and I'm trying to start with the very basics to get a handle on them. I'm trying to write code to get the...
4
by: jmarcrum | last post by:
I have to modify this assembly code in order to make it use interrupts. Right now, when I push button 1 on the microprocessor the count increments once (the LED's count in binary, using only 3 LED's...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.