+ Reply to Thread
Results 1 to 9 of 9

Use Event Procedure and OnTime to Stop a Macro

  1. #1
    Registered User
    Join Date
    08-30-2008
    Location
    USA
    Posts
    30

    Use Event Procedure and OnTime to Stop a Macro

    I have an app that uses Windows timer for gathering data. It uses the pop timer code in a macro that I can start at will. I have looked for a way to use OnTime Method to automatically stop the macro after a period of time has expired... but to no avail. I can start my macros at certain time of day, etc., but I have found nothing that sets a timer and then does some action (e.g. killtimer) after some duration of time has elapsed. I need to limit the data collection to 960 cells. This equates to a 4 hour trend @ 15 second intervals.

    I have found some code that uses eventprocedure and ontime method to close a workbook after some definable period of time.
    This raises the question, why could I not use something similar to this to stop collecting data e.g. killtimer to stop my macro?? Anyone have some idea which direction I should take on this?

    Thanks again for the very fine help I have experienced on this forum. One of these days I hope to know enough to be a resource to someone else as you all have been to me.

  2. #2
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    hi,

    You could try Googling because there are timer options around (you may need to allow for the impact of midnight).
    Or, you could use a counter as shown by the below example from the Excel 2003 Help files:

    Do...Loop Statement Example
    This example shows how Do...Loop statements can be used. The inner Do...Loop statement loops 10 times, sets the value of the flag to False, and exits prematurely using the Exit Do statement. The outer loop exits immediately upon checking the value of the flag.

    Please Login or Register  to view this content.
    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  3. #3
    Registered User
    Join Date
    08-30-2008
    Location
    USA
    Posts
    30
    Good morning, Rob. Thank you for your reply. I have three questions about the counter. First, to what time base is the counter triggered? The time duration for my macro to run needs to be somewhat reasonably accurate to at least within 1 minute. Second, what actually starts this counter? And last, could you give me and idea how this code would be integrated to stop my macro at the end of the counter duration "check". Thanks.

  4. #4
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    hi,

    I may be on the wrong track here...

    1) The counter approach doesn't need a time base, 2) you can link it to the number of cells that have had data placed in them (I'm assuming from the op that you already have the code using the window timer which populates the cells every 15 seconds).
    3) I would put your code immediately after the line "Do ' Outer loop.".

    Can you please attach a sample workbook with your current code?

    hth
    Rob

  5. #5
    Registered User
    Join Date
    08-30-2008
    Location
    USA
    Posts
    30
    Thank you, Rob for your reply. Following is my code. In my main worksheet is the following start/stop and cleanup code:
    Please Login or Register  to view this content.
    And here this code located in a Module:

    Please Login or Register  to view this content.
    There is some other macros/code that is used to delete data and give me a blank worksheet to start new data collection.

    As for my question about the time base... duh! We are talking about a counter here. Oops.
    I can understand how using a counter to count the number of cell entries on my "Value1" sheet could be used to killtimer. I guess that is what I need to understand. Your help is much appreciated.

  6. #6
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    hi,

    I'm pleased I could help :-)


    Your code can be shortened slightly by removing or merging the ".select" & the "selection." sections. I haven't entered any code for the counter because I'm not too sure where it needs to go.

    [QUOTE
    Please Login or Register  to view this content.
    [/QUOTE]

    Also, on the off-chance that you may change to Excel 2007 in the foreseeable future I would change:
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    because this allows for the fact that there are more than 65K rows in Excel 2007.


    Now, my turn for a question
    Where did you learn the syntax for the brackets at the end of the range address?
    I don't think I've ever seen it before but it looks interesting & I'd like to learn more about it.

    Let us know if you need any more clarification around the use of a counter...

    hth
    Rob

  7. #7
    Registered User
    Join Date
    08-30-2008
    Location
    USA
    Posts
    30
    Good morning, Rob: Sorry for the delay in getting back here. It has been very busy at workee and just now got the opportunity to check back. Thanks for the abbreviation and suggestion for my code.

    The brackets in the Range addy I think I picked up from vb code examples I found to use between Windows apps and Allen Bradley PLCs. We use a bit of DDE over a communications highway to peek and poke data between PLCs and MS Excel, MS Access, etc. It is possible to use an Excel cell or range of cells as an object and refer to the object in RSLogix formulas...thus the bracketed addy. This make sense?

    I use MS Excel as a switchboard for operators to use to view (and in some cases makes program changes in) a large number of production area controllers.
    I am not really proficient in VB and so I ask a lot of questions while I learn it. Andy Pope has been a huge help as I need to move along quickly on some of these projects. One of these days I hope to get a good grip on VBA and VB. This is a great forum for me.
    Again, thanks for your help.

  8. #8
    Registered User
    Join Date
    08-30-2008
    Location
    USA
    Posts
    30
    Oh.. and I almost forgot.. regarding my question about what time base is used for the counter?

    As I got to thinking about your response, I realized that a bit of PLC programming mindset had crept into my thinking. Counters don't typically count up or stay "enabled" on their own. They are conditioned by inputs that trigger (enable) them. In regards to my data collection, an accumulated value in a counter would work fine to set a line in a "stop macro" vb line. The countup of 1 count at the end of each 15 second pop timer interval would be the time base (in my clogged mind way of thinking).
    I will have to do some more work on killing the timer to make this work. I am trying to get the data collection and logging/graphing to run for 4 hours and then stop all on its own. I even thought of using some logic in the PLC from which I am sampling data to set a bit... Excel to update a cell with the condition in the address... then use the cell state (an event) to stop the macro. I'll have to give it some thought. I may be making this more complicated than I realize is necessary.

    chow

  9. #9
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243
    hi

    Asking questions is good & I'd say Andy could be much more help than I - esp considering I don't understand the TimerProc well enough to get it to work when testing at home.
    The section between the asterisks shows the principle (check the startcell, id an endcell & run until then) I'd use to stop the macro when enough cells are filled, but I don't know how you are looping at the moment...

    Also, I have returned the error to zero in the second commandbutton.

    Please Login or Register  to view this content.
    hth
    Rob

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1