Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 96
Default Infinite loop? Help.

I am trying to get some code to work and I think it goes into an infinite loop. Can anyone tell me what I am doing wrong? Code follows:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim aNames As Collection
Dim bNames As Collection
Dim c As Range
Dim rng As Range
Dim iCt As Integer
Dim iRow As Integer
Dim Ct As Integer


On Error Resume Next

For iRow = 3 To 37 Step 2
Set aNames = New Collection
Set bNames = New Collection
Set rng = Sheets("Tracker").Range("G" & iRow & ":CI" & iRow)
For Each c In rng
Debug.Print c.Address
For Ct = 1 To 18
If c.Value = Sheets("Weekly Sched").Cells(Ct + 4, 10) Then
aNames.Add c.Value, c.Value
End If
Next Ct
If aNames.Count = 0 Then
bNames.Add c.Value, c.Value
End If
Next c

Sheets("Tracker").Cells(iRow - 1, 4) = aNames.Count
Sheets("Tracker").Cells(iRow, 4) = bNames.Count
Sheets("Test").Cells((iRow - 1) / 2 + 4, 6) = aNames.Count + bNames.Count

Set aNames = Nothing
Set bNames = Nothing

Next iRow

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default Infinite loop? Help.

A hypothesis is that you have this EVENT procedure in the
code module for the sheet Tracker or Test. With the name
of the procedu
Private Sub Worksheet_Change(ByVal Target As Range)
and having these lines in it:
Sheets("Tracker").Cells(iRow - 1, 4) = aNames.Count
Sheets("Tracker").Cells(iRow, 4) = bNames.Count
Sheets("Test").Cells((iRow - 1) / 2 + 4, 6) = . . .
the procedure will be initiated again whenever it hits one
of these lines. I suspect you should put the code (with a
different name) in a separate module (not a worksheet
module).

HTH,
Merjet



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 96
Default Infinite loop? Help.

That was the problem. I don't know why I couldn't see that. Thanks.

"merjet" wrote:

A hypothesis is that you have this EVENT procedure in the
code module for the sheet Tracker or Test. With the name
of the procedu
Private Sub Worksheet_Change(ByVal Target As Range)
and having these lines in it:
Sheets("Tracker").Cells(iRow - 1, 4) = aNames.Count
Sheets("Tracker").Cells(iRow, 4) = bNames.Count
Sheets("Test").Cells((iRow - 1) / 2 + 4, 6) = . . .
the procedure will be initiated again whenever it hits one
of these lines. I suspect you should put the code (with a
different name) in a separate module (not a worksheet
module).

HTH,
Merjet




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 863
Default Infinite loop? Help.

Or disable events before executing statements that change cells, i.e.

Application.EnableEvents = False

On Sat, 31 Jul 2004 20:03:02 -0700, "Erik"
wrote:

That was the problem. I don't know why I couldn't see that. Thanks.

"merjet" wrote:

A hypothesis is that you have this EVENT procedure in the
code module for the sheet Tracker or Test. With the name
of the procedu
Private Sub Worksheet_Change(ByVal Target As Range)
and having these lines in it:
Sheets("Tracker").Cells(iRow - 1, 4) = aNames.Count
Sheets("Tracker").Cells(iRow, 4) = bNames.Count
Sheets("Test").Cells((iRow - 1) / 2 + 4, 6) = . . .
the procedure will be initiated again whenever it hits one
of these lines. I suspect you should put the code (with a
different name) in a separate module (not a worksheet
module).

HTH,
Merjet





  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Infinite loop? Help.

Hi
at the beginning of your code add:
application.enableevents=false

and at the end
application.enableevents=True

BUT: I'm not so sure why you run this code each time a value is changed
in your sheet?


--
Regards
Frank Kabel
Frankfurt, Germany


Erik wrote:
I am trying to get some code to work and I think it goes into an
infinite loop. Can anyone tell me what I am doing wrong? Code
follows:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim aNames As Collection
Dim bNames As Collection
Dim c As Range
Dim rng As Range
Dim iCt As Integer
Dim iRow As Integer
Dim Ct As Integer


On Error Resume Next

For iRow = 3 To 37 Step 2
Set aNames = New Collection
Set bNames = New Collection
Set rng = Sheets("Tracker").Range("G" & iRow & ":CI" & iRow)
For Each c In rng
Debug.Print c.Address
For Ct = 1 To 18
If c.Value = Sheets("Weekly Sched").Cells(Ct + 4, 10) Then
aNames.Add c.Value, c.Value
End If
Next Ct
If aNames.Count = 0 Then
bNames.Add c.Value, c.Value
End If
Next c

Sheets("Tracker").Cells(iRow - 1, 4) = aNames.Count
Sheets("Tracker").Cells(iRow, 4) = bNames.Count
Sheets("Test").Cells((iRow - 1) / 2 + 4, 6) = aNames.Count +
bNames.Count

Set aNames = Nothing
Set bNames = Nothing

Next iRow

End Sub




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Infinite loop? Help.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim aNames As Collection
Dim bNames As Collection
Dim c As Range
Dim rng As Range
Dim iCt As Integer
Dim iRow As Integer
Dim Ct As Integer


On Error goto ErrHandler

For iRow = 3 To 37 Step 2
Set aNames = New Collection
Set bNames = New Collection
Set rng = Sheets("Tracker").Range("G" & iRow & ":CI" & iRow)
For Each c In rng
Debug.Print c.Address
For Ct = 1 To 18
If c.Value = Sheets("Weekly Sched").Cells(Ct + 4, 10) Then
On Error Resume Next
aNames.Add c.Value, c.Value
On Error goto ErrHandler
End If
Next Ct
If aNames.Count = 0 Then
On Error Resume Next
bNames.Add c.Value, c.Value
On Error goto ErrHandler
End If
Next c
Application.EnableEvents = False
Sheets("Tracker").Cells(iRow - 1, 4) = aNames.Count
Sheets("Tracker").Cells(iRow, 4) = bNames.Count
Sheets("Test").Cells((iRow - 1) / 2 + 4, 6) = aNames.Count +
bNames.Count

Set aNames = Nothing
Set bNames = Nothing

Next iRow
ErrHandler:
Application.EnableEvents = True
End Sub

--
Regards,
Tom Ogilvy

"Erik" wrote in message
...
I am trying to get some code to work and I think it goes into an infinite

loop. Can anyone tell me what I am doing wrong? Code follows:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim aNames As Collection
Dim bNames As Collection
Dim c As Range
Dim rng As Range
Dim iCt As Integer
Dim iRow As Integer
Dim Ct As Integer


On Error Resume Next

For iRow = 3 To 37 Step 2
Set aNames = New Collection
Set bNames = New Collection
Set rng = Sheets("Tracker").Range("G" & iRow & ":CI" & iRow)
For Each c In rng
Debug.Print c.Address
For Ct = 1 To 18
If c.Value = Sheets("Weekly Sched").Cells(Ct + 4, 10) Then
aNames.Add c.Value, c.Value
End If
Next Ct
If aNames.Count = 0 Then
bNames.Add c.Value, c.Value
End If
Next c

Sheets("Tracker").Cells(iRow - 1, 4) = aNames.Count
Sheets("Tracker").Cells(iRow, 4) = bNames.Count
Sheets("Test").Cells((iRow - 1) / 2 + 4, 6) = aNames.Count +

bNames.Count

Set aNames = Nothing
Set bNames = Nothing

Next iRow

End Sub



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT- Can't get out of infinite loop J@Y Excel Discussion (Misc queries) 4 June 14th 07 09:41 PM
Infinite loop using Worksheet_Calculate Parker Excel Discussion (Misc queries) 2 November 3rd 06 12:25 PM
For Next Infinite Loop Naji Excel Discussion (Misc queries) 5 January 13th 06 06:56 PM
HELP!!!! Can't stop a loop (NOT an infinite loop) TBA[_2_] Excel Programming 3 December 14th 03 03:33 PM
Infinite Loop Steve Wylie Excel Programming 1 December 3rd 03 02:02 PM


All times are GMT +1. The time now is 10:21 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"