Date: Fri, 12 Oct 2007 16:24:09 -0500 (CDT) From: Mark Jacobson To: 810-022-01-fall@uni.edu Subject: doPause() Sub Hi 022 students, Here is an attempt to relate the doPause() Sub to everyday life and running or doing elliptical at the WRC for 10 minutes or deciding to study for 30 minutes and having stopTime be 30 minutes from NOW. ----- NOW is Timer in VBA macros! ----- Sub doPauseTenMinutes() 1. stopTime will be set to be 1 second from now. (Look at the clock, and add 600 seconds to the time, i.e. at 10 minutes). 2. currentTime will be set to whatever time the clock says now. In Visual Basic, the clock or watch you just looked at in your room or office or at the library or Union is called Timer. Timer returns the number of seconds since midnight. If you looked at your watch right now, or your kitchen or desk clock, and it was like Timer, it would say: ?Timer 57043.38 57,043 if it was about 3:55 p.m. 57,043 seconds since midnight ?Timer / 3600 15.84783 <----------- 15.848 hours since midnight 3. Do the following While currentTime is LESS THAN stopTime 3.1 Look at the clock again, i.e. update the currentTime, i.e. currentTime = Timer is you looking at your watch or clock again to see what time it is NOW. 3.2 DoEvents remember that we had this in our Twirling WordArt program. It just makes the VBA application cooperate with Excel better, and allows you to break out of the program, etc. Loop (this is the END of the Loop) When the Loop line is reached, the BODY of the DO WHILE loop has been executed. We return back up to the TOP of the Do While loop, and perform the test. Do While currentTime < stopTime ----------------------- This is the condition, the test. currentTime < stopTime is either TRUE or FALSE ---------------------- I want to RUN/JOG for 10 minutes. What do I do? 1. Look at my watch and add ten minutes to what time it is, and record that in my memory as the stopTime stopTime = 4:15:00 p.m. + 10 <--- so stopTime = 4:25 p.m. 2. Get the currentTime by looking at my watch: currentTime = 4:15:12 p.m. 3. Do While currentTime < stopTime keep running and looking at my watch constantly, if I were a dumb computer and couldn't just look every few minutes till it got close to the stopTime, when I would look very often, many times per minute in the last minute or so. 3.1 currentTime = whatever <----- this is getting the new TIME that it is NOW, since the last time I looked... 3.2 DoEvents Loop Sub doPauseOneSecond() stopTime = Timer + 1 currentTime = Timer <------- get FIRST time... Do While currentTime < stopTime currentTime = Timer <------- get NEXT time... DoEvents Loop End Sub Sub doWhileLoopDemo() i = 1 <---- For i = 1 to 20 For Next loop would NOT need this extra line. Do While i <= 20 Cells(i, i).Value = i * i i = i + 1 <------- Next i does this for you! Loop End Sub ?12 ^ 2 View menu, Immediate Window again to 144 2 demonstrate that 12 ^ 2 12 = 144 ?2 ^ 10 1024 is identical to 12 * 12 ?9 ^ 2 81 Sub doWhileLoopDemo() i = 1 <------------- Not needed in For Next loop Do While i <= 20 Cells(i, i).Value = i * i ' i * i is same as i ^ 2 i = i + 1 <--------- Not needed in For Next loop Loop End Sub Sub forNextLoopExactlySameAsDoWhile() For i = 1 To 20 Cells(i, i).Value = 2 * i ' Multiply by 2 instead of Square i Next i End Sub Hopefully, everyone will do perfect on the pause() Sub question on Monday now. I will post this email note on our web page study guide at: http://cns2.uni.edu/~jacobson/test1Guide022Fall2007.html http://cns2.uni.edu/~jacobson/c022.html Happy UNI homecoming. See you on Monday. Mark