+ Reply to Thread
Results 1 to 9 of 9

Use Event Procedure and OnTime to Stop a Macro

Hybrid View

Protiusmime Use Event Procedure and... 09-14-2008, 04:24 AM
broro183 hi, You could try Googling... 09-14-2008, 05:31 AM
Protiusmime Good morning, Rob. Thank you... 09-14-2008, 07:12 AM
broro183 hi, I may be on the wrong... 09-14-2008, 07:46 AM
Protiusmime Thank you, Rob for your... 09-15-2008, 12:44 AM
broro183 hi, I'm pleased I could... 09-15-2008, 04:23 AM
Protiusmime Good morning, Rob: Sorry for... 09-26-2008, 06:31 AM
Protiusmime Oh.. and I almost forgot..... 09-26-2008, 06:42 AM
broro183 hi Asking questions is... 09-26-2008, 07:46 PM
  1. #1
    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
    Private Sub CommandButton1_Click()
    
        TimerSeconds = 15                   
        TimerID = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf TimerProc)
        
        Range("G6").ClearContents
    'rename the sheet on the rh side of the equals sign as needed
        Sheets("Source").Range("F6").value = sheets("?").Range("F10").value
        Range("C2").Select
       
    End Sub
    
    Private Sub CommandButton2_Click()
        On Error Resume Next
        KillTimer 0&, TimerID
        
        Range("F6").ClearContents
        Range("G6").value = Range("F10").value
        Range("C2").Select
    End Sub
    [/QUOTE]

    Also, on the off-chance that you may change to Excel 2007 in the foreseeable future I would change:
    .Sheets("Value1").Range("A65536").End(xlUp)(2, 1)
    to
    .Sheets("Value1").cells(rows.count,1).End(xlUp)(2, 1)
    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
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  2. #2
    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.

  3. #3
    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

  4. #4
    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.

    Option Explicit
    Public Declare Function SetTimer Lib "user32" ( _
        ByVal HWnd As Long, ByVal nIDEvent As Long, _
        ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    Public Declare Function KillTimer Lib "user32" ( _
        ByVal HWnd As Long, ByVal nIDEvent As Long) As Long
    Public TimerID As Long
    Public TimerSeconds As Single
    
    Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, _
        ByVal nIDEvent As Long, ByVal dwTimer As Long)
        
    With ThisWorkbook
        .Sheets("Value1").Cells(Rows.Count, 1).End(xlUp)(2, 1) = .Sheets("Source").Range("B3")
        .Sheets("Value1").Cells(Rows.Count, 1).End(xlUp)(1, 3) = .Sheets("Source").Range("B4")
        .Sheets("Value1").Cells(Rows.Count, 1).End(xlUp)(1, 5) = .Sheets("Source").Range("B5")
        .Sheets("Value1").Cells(Rows.Count, 1).End(xlUp)(1, 8) = .Sheets("Source").Range("C2")
        .Sheets("Value1").Cells(Rows.Count, 1).End(xlUp)(1, 10) = .Sheets("Source").Range("B7")
    End With
    End Sub
    
    Private Sub CommandButton1_Click()
    '**************************************************
    Const RepeatsPlusOne As Long = (4 * 60 * 4) + 1
    '=4 hrs * 60 min * 4 times per min + 1 to make sure the last log is made
    Dim StartCell As Range
    Dim EndCell As Range
    
    Set StartCell = ThisWorkbook.Sheets("Value1").Cells(Rows.Count, 1).End(xlUp)(2, 1)
    Set EndCell = StartCell.Offset(RepeatsPlusOne, 0)
    If ThisWorkbook.Sheets("Value1").Cells(Rows.Count, 1).End(xlUp)(2, 1).Address = EndCell.Address Then Exit Sub
    
    ' or maybe uncomment the ###
    '### Do Until ThisWorkbook.Sheets("Value1").Cells(Rows.Count, 1).End(xlUp)(2, 1).Address = EndCell.Address
    '**************************************************
    
    TimerSeconds = 15
        TimerID = SetTimer(0&, 0&, TimerSeconds * 1000&, AddressOf TimerProc)
        
        Range("G6").ClearContents
    'rename the sheet on the rh side of the equals sign as needed
        Sheets("Source").Range("F6").Value = Sheets("value1").Range("F10").Value
        Range("C2").Select
    '### loop
    
    End Sub
    
    Private Sub CommandButton2_Click()
        On Error Resume Next
            KillTimer 0&, TimerID
        On Error GoTo 0 ' added to ensure the error action is reset
        Range("F6").ClearContents
        Range("G6").Value = Range("F10").Value
        Range("C2").Select
    End Sub
    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