Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Recursive Loop -- How to Stop It?
My question involves an Excel input file where people are supposed to enter
certain data ONLY on Thursday thru Sunday. (1) On Save, ThisWorkbook code checks the day of the week, and if it's outside that window, opens UserForm1 with a reminder message. This works. (2) When the form activates, its code kicks off macro Flash1, which is intentionally recursive, switching the reminder text between black and red once per second to get the user's attention. This works too. (4) the form's <OK button sets a global boolean to True and hides the form. The boolean, in turn, SHOULD affect the next loop of the Flash1, causing it to terminate, but it doesn't -- it keeps on running in the background. (I also tried unloading the form rather than just hiding it, but no good either.) Can anybody tell me what I'm doing wrong? I've done similar things with Access's Timer Event, but Excel's Application.OnTime apparently isn't quite equivalent. (Full code follows.) <<<ThisWorkbook Code Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) QuitNow = False If Weekday(Date) < 4 Then UserForm1.Show End Sub <<<UserForm1 Code Private Sub UserForm_Activate() UserForm1.Label1.ForeColor = vbBlack NTime = Now + TimeValue("00:00:01") Application.OnTime NTime, "Flash1" 'runs Flash1 the FIRST time, after which it runs itself recursively End Sub Private Sub cmdOK_Click() QuitNow = True €˜seems like this should stop the Flash1 recursive loop, but it doesnt€¦ UserForm1.Hide End Sub <<<MACRO CODE Dim NTime As Date, QuitNow As Boolean €˜Global variables€¦ Sub Flash1() If QuitNow = True Then GoTo StopNow NTime = Now + TimeValue("00:00:01") If UserForm1.Label1.ForeColor = vbBlack Then UserForm1.Label1.ForeColor = vbRed Else UserForm1.Label1.ForeColor = vbBlack End If Application.OnTime NTime, "Flash1" StopNow: End Sub |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Recursive Loop -- How to Stop It?
"exit sub" an idea?
"LarryP" schreef in bericht ... My question involves an Excel input file where people are supposed to enter certain data ONLY on Thursday thru Sunday. (1) On Save, ThisWorkbook code checks the day of the week, and if it's outside that window, opens UserForm1 with a reminder message. This works. (2) When the form activates, its code kicks off macro Flash1, which is intentionally recursive, switching the reminder text between black and red once per second to get the user's attention. This works too. (4) the form's <OK button sets a global boolean to True and hides the form. The boolean, in turn, SHOULD affect the next loop of the Flash1, causing it to terminate, but it doesn't -- it keeps on running in the background. (I also tried unloading the form rather than just hiding it, but no good either.) Can anybody tell me what I'm doing wrong? I've done similar things with Access's Timer Event, but Excel's Application.OnTime apparently isn't quite equivalent. (Full code follows.) <<<ThisWorkbook Code Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) QuitNow = False If Weekday(Date) < 4 Then UserForm1.Show End Sub <<<UserForm1 Code Private Sub UserForm_Activate() UserForm1.Label1.ForeColor = vbBlack NTime = Now + TimeValue("00:00:01") Application.OnTime NTime, "Flash1" 'runs Flash1 the FIRST time, after which it runs itself recursively End Sub Private Sub cmdOK_Click() QuitNow = True 'seems like this should stop the Flash1 recursive loop, but it doesn't. UserForm1.Hide End Sub <<<MACRO CODE Dim NTime As Date, QuitNow As Boolean 'Global variables. Sub Flash1() If QuitNow = True Then GoTo StopNow NTime = Now + TimeValue("00:00:01") If UserForm1.Label1.ForeColor = vbBlack Then UserForm1.Label1.ForeColor = vbRed Else UserForm1.Label1.ForeColor = vbBlack End If Application.OnTime NTime, "Flash1" StopNow: End Sub |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Recursive Loop -- How to Stop It?
Tried that too without success.
"Gert-Jan" wrote: "exit sub" an idea? "LarryP" schreef in bericht ... My question involves an Excel input file where people are supposed to enter certain data ONLY on Thursday thru Sunday. (1) On Save, ThisWorkbook code checks the day of the week, and if it's outside that window, opens UserForm1 with a reminder message. This works. (2) When the form activates, its code kicks off macro Flash1, which is intentionally recursive, switching the reminder text between black and red once per second to get the user's attention. This works too. (4) the form's <OK button sets a global boolean to True and hides the form. The boolean, in turn, SHOULD affect the next loop of the Flash1, causing it to terminate, but it doesn't -- it keeps on running in the background. (I also tried unloading the form rather than just hiding it, but no good either.) Can anybody tell me what I'm doing wrong? I've done similar things with Access's Timer Event, but Excel's Application.OnTime apparently isn't quite equivalent. (Full code follows.) <<<ThisWorkbook Code Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) QuitNow = False If Weekday(Date) < 4 Then UserForm1.Show End Sub <<<UserForm1 Code Private Sub UserForm_Activate() UserForm1.Label1.ForeColor = vbBlack NTime = Now + TimeValue("00:00:01") Application.OnTime NTime, "Flash1" 'runs Flash1 the FIRST time, after which it runs itself recursively End Sub Private Sub cmdOK_Click() QuitNow = True 'seems like this should stop the Flash1 recursive loop, but it doesn't. UserForm1.Hide End Sub <<<MACRO CODE Dim NTime As Date, QuitNow As Boolean 'Global variables. Sub Flash1() If QuitNow = True Then GoTo StopNow NTime = Now + TimeValue("00:00:01") If UserForm1.Label1.ForeColor = vbBlack Then UserForm1.Label1.ForeColor = vbRed Else UserForm1.Label1.ForeColor = vbBlack End If Application.OnTime NTime, "Flash1" StopNow: End Sub |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Recursive Loop -- How to Stop It?
|
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Recursive Loop -- How to Stop It?
Hi LarryP
The only change i made was to declare both variables Public and it worked fine for me - assuming the cmdbutton is on the form of course. Geoff "LarryP" wrote: Tried that too without success. "Gert-Jan" wrote: "exit sub" an idea? "LarryP" schreef in bericht ... My question involves an Excel input file where people are supposed to enter certain data ONLY on Thursday thru Sunday. (1) On Save, ThisWorkbook code checks the day of the week, and if it's outside that window, opens UserForm1 with a reminder message. This works. (2) When the form activates, its code kicks off macro Flash1, which is intentionally recursive, switching the reminder text between black and red once per second to get the user's attention. This works too. (4) the form's <OK button sets a global boolean to True and hides the form. The boolean, in turn, SHOULD affect the next loop of the Flash1, causing it to terminate, but it doesn't -- it keeps on running in the background. (I also tried unloading the form rather than just hiding it, but no good either.) Can anybody tell me what I'm doing wrong? I've done similar things with Access's Timer Event, but Excel's Application.OnTime apparently isn't quite equivalent. (Full code follows.) <<<ThisWorkbook Code Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) QuitNow = False If Weekday(Date) < 4 Then UserForm1.Show End Sub <<<UserForm1 Code Private Sub UserForm_Activate() UserForm1.Label1.ForeColor = vbBlack NTime = Now + TimeValue("00:00:01") Application.OnTime NTime, "Flash1" 'runs Flash1 the FIRST time, after which it runs itself recursively End Sub Private Sub cmdOK_Click() QuitNow = True 'seems like this should stop the Flash1 recursive loop, but it doesn't. UserForm1.Hide End Sub <<<MACRO CODE Dim NTime As Date, QuitNow As Boolean 'Global variables. Sub Flash1() If QuitNow = True Then GoTo StopNow NTime = Now + TimeValue("00:00:01") If UserForm1.Label1.ForeColor = vbBlack Then UserForm1.Label1.ForeColor = vbRed Else UserForm1.Label1.ForeColor = vbBlack End If Application.OnTime NTime, "Flash1" StopNow: End Sub |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Recursive Loop -- How to Stop It?
"Forest for the trees"! That did it. Thanks.
"Geoff" wrote: Hi LarryP The only change i made was to declare both variables Public and it worked fine for me - assuming the cmdbutton is on the form of course. Geoff "LarryP" wrote: Tried that too without success. "Gert-Jan" wrote: "exit sub" an idea? "LarryP" schreef in bericht ... My question involves an Excel input file where people are supposed to enter certain data ONLY on Thursday thru Sunday. (1) On Save, ThisWorkbook code checks the day of the week, and if it's outside that window, opens UserForm1 with a reminder message. This works. (2) When the form activates, its code kicks off macro Flash1, which is intentionally recursive, switching the reminder text between black and red once per second to get the user's attention. This works too. (4) the form's <OK button sets a global boolean to True and hides the form. The boolean, in turn, SHOULD affect the next loop of the Flash1, causing it to terminate, but it doesn't -- it keeps on running in the background. (I also tried unloading the form rather than just hiding it, but no good either.) Can anybody tell me what I'm doing wrong? I've done similar things with Access's Timer Event, but Excel's Application.OnTime apparently isn't quite equivalent. (Full code follows.) <<<ThisWorkbook Code Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) QuitNow = False If Weekday(Date) < 4 Then UserForm1.Show End Sub <<<UserForm1 Code Private Sub UserForm_Activate() UserForm1.Label1.ForeColor = vbBlack NTime = Now + TimeValue("00:00:01") Application.OnTime NTime, "Flash1" 'runs Flash1 the FIRST time, after which it runs itself recursively End Sub Private Sub cmdOK_Click() QuitNow = True 'seems like this should stop the Flash1 recursive loop, but it doesn't. UserForm1.Hide End Sub <<<MACRO CODE Dim NTime As Date, QuitNow As Boolean 'Global variables. Sub Flash1() If QuitNow = True Then GoTo StopNow NTime = Now + TimeValue("00:00:01") If UserForm1.Label1.ForeColor = vbBlack Then UserForm1.Label1.ForeColor = vbRed Else UserForm1.Label1.ForeColor = vbBlack End If Application.OnTime NTime, "Flash1" StopNow: End Sub |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Recursive Loop -- How to Stop It?
Hi LarryP
Pleased to help <g I find using Option Explicit in each module is very helpful for trapping such errors. Geoff "LarryP" wrote: "Forest for the trees"! That did it. Thanks. "Geoff" wrote: Hi LarryP The only change i made was to declare both variables Public and it worked fine for me - assuming the cmdbutton is on the form of course. Geoff "LarryP" wrote: Tried that too without success. "Gert-Jan" wrote: "exit sub" an idea? "LarryP" schreef in bericht ... My question involves an Excel input file where people are supposed to enter certain data ONLY on Thursday thru Sunday. (1) On Save, ThisWorkbook code checks the day of the week, and if it's outside that window, opens UserForm1 with a reminder message. This works. (2) When the form activates, its code kicks off macro Flash1, which is intentionally recursive, switching the reminder text between black and red once per second to get the user's attention. This works too. (4) the form's <OK button sets a global boolean to True and hides the form. The boolean, in turn, SHOULD affect the next loop of the Flash1, causing it to terminate, but it doesn't -- it keeps on running in the background. (I also tried unloading the form rather than just hiding it, but no good either.) Can anybody tell me what I'm doing wrong? I've done similar things with Access's Timer Event, but Excel's Application.OnTime apparently isn't quite equivalent. (Full code follows.) <<<ThisWorkbook Code Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) QuitNow = False If Weekday(Date) < 4 Then UserForm1.Show End Sub <<<UserForm1 Code Private Sub UserForm_Activate() UserForm1.Label1.ForeColor = vbBlack NTime = Now + TimeValue("00:00:01") Application.OnTime NTime, "Flash1" 'runs Flash1 the FIRST time, after which it runs itself recursively End Sub Private Sub cmdOK_Click() QuitNow = True 'seems like this should stop the Flash1 recursive loop, but it doesn't. UserForm1.Hide End Sub <<<MACRO CODE Dim NTime As Date, QuitNow As Boolean 'Global variables. Sub Flash1() If QuitNow = True Then GoTo StopNow NTime = Now + TimeValue("00:00:01") If UserForm1.Label1.ForeColor = vbBlack Then UserForm1.Label1.ForeColor = vbRed Else UserForm1.Label1.ForeColor = vbBlack End If Application.OnTime NTime, "Flash1" StopNow: End Sub |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
close workbook without closing excel and stop recursive function | Excel Discussion (Misc queries) | |||
Hot key to stop a LOOP | Excel Programming | |||
how to stop program in a big loop? | Excel Programming | |||
how to stop a loop | Excel Programming | |||
HELP!!!! Can't stop a loop (NOT an infinite loop) | Excel Programming |