Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Macro entering an infinite loop

I am trying to run a simple copy and paste macro when a cell changes
from 0 to 1. I have the following code for my worksheet:

Private Sub Worksheet_Calculate()

Static OldVal As Integer
If OldVal = 0 Then
If Range("E5").Value = 1 Then
Call test
End If
End If
OldVal = Range("E5").Value

End Sub


This code should just run the macro called "test" when E5 changes from
0 to 1.
My "test" macro is a module in my workbook:


Sub test()

Dim rng As Range

Set rng = Workbooks("Data").Sheets("Limits") _
.Cells(Rows.count, 1).End(xlUp)(2).Offset(3, 0)

Sheets("Form").Select
Range("A3:B5").Copy
rng.PasteSpecial xlFormats
rng.PasteSpecial xlValues

Application.CutCopyMode = False

End Sub

This should copy cells A3 to B5 and paste formats and values into the
"Limits" sheet 3 rows down from the last entry. The test macro runs
fine when I just run it manually. However, when cell E5 changes, the
code seems to run into an infinite loop. It pastes the formats but then
just seems to switch back and forth between the 'limits' and 'form'
worksheets. Anyone know why this could be? I just want the macro to
run once, then stop. Thanks for any help you can give.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Macro entering an infinite loop

It is probably cascading events, the calculate event gets called, which
changes the sheet, which calls the calculate event, which changes the sheet,
.....

Try

Private Sub Worksheet_Calculate()

Static OldVal As Integer
On Error Goto ws_exit
Application.EnableEvents = False
If OldVal = 0 Then
If Range("E5").Value = 1 Then
Call test
End If
End If
OldVal = Range("E5").Value

ws_exit:
Application.EnableEvents = True
End Sub



--

HTH

RP
(remove nothere from the email address if mailing direct)


wrote in message
ups.com...
I am trying to run a simple copy and paste macro when a cell changes
from 0 to 1. I have the following code for my worksheet:

Private Sub Worksheet_Calculate()

Static OldVal As Integer
If OldVal = 0 Then
If Range("E5").Value = 1 Then
Call test
End If
End If
OldVal = Range("E5").Value

End Sub


This code should just run the macro called "test" when E5 changes from
0 to 1.
My "test" macro is a module in my workbook:


Sub test()

Dim rng As Range

Set rng = Workbooks("Data").Sheets("Limits") _
.Cells(Rows.count, 1).End(xlUp)(2).Offset(3, 0)

Sheets("Form").Select
Range("A3:B5").Copy
rng.PasteSpecial xlFormats
rng.PasteSpecial xlValues

Application.CutCopyMode = False

End Sub

This should copy cells A3 to B5 and paste formats and values into the
"Limits" sheet 3 rows down from the last entry. The test macro runs
fine when I just run it manually. However, when cell E5 changes, the
code seems to run into an infinite loop. It pastes the formats but then
just seems to switch back and forth between the 'limits' and 'form'
worksheets. Anyone know why this could be? I just want the macro to
run once, then stop. Thanks for any help you can give.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Macro entering an infinite loop

Bob,

That works! I think you were right, when the formats were pasted the
sheet recalculates because there was a change. Thus the macro starts
over and so on. Your code worked great for me, thanks for the quick
reply.

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
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
Infinite loop? Help. Erik Excel Programming 5 August 1st 04 05:11 AM
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 06:29 PM.

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"