+ Reply to Thread
Results 1 to 8 of 8

Excel Macro DO/LOOP not working after cells update from another application

Hybrid View

  1. #1
    Registered User
    Join Date
    06-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2000
    Posts
    13

    Smile Excel Macro DO/LOOP not working after cells update from another application

    Help please! I have a macro (below) that works OK when invoked manually for grabbing data from a few cells as they get automatically updated from another application. My objective is to detect when new data arrives and continuously update a historical data table for the last N data points. My problem is that I can’t get Do/Loop to do this automatically (as if the macro gets interrupted by the updates.)

    In the code below I am downloading two variables-&-time/date (each updated at different time intervals).
    -- The first two routines check if data is new and is so will update the historical data table.
    -- “Sub DownloadUpdate()” works OK when I invoke it manually.
    -- “Sub UpdateTable()” also works OK manually BUT ONLY ONCE, i.e. I can’t get Do/Loop to do this automatically

    I sure would appreciate some guidance. (I’m pretty good with Excel but only a novice with macros.) Thanks guys,

    Tom

    PS – Here is my code:

    Sub NewMinClose()
    '
    
    
    ' NewMinClose Macro
    ' Check to see if new Close data has arrived by comparing current & grabbed times
    ' If it is NEW then UPDATE THE TABLE and also paste the new data in the "grabbed" cells
    ' Range D13 is the TIME of the latestdata available
        If Range("D13") <> Range("D6") Then
        Range("D9").Value = "New"
        Else: Range("D9").Value = ""
        End If
    
    ' If data is NOT new then end this routine
        If Range("D9") = "" Then End
        
    ' If the data IS new then update table:
            ' Shift Minute data from the Second (table) Row up to the First Row
            ' Copy/Paste grabbed Minute data to the last Row
        
    ' Range B18-D26 will need to change: B18:G517 (for 500 data lines)
        Range("B18:D26").Select
        Selection.Cut
        Range("B17").Select
        ActiveSheet.Paste
        Range("B13:D13").Select
        Selection.Copy
    ' Range B26 will need to change to B517 in order to post on the 500th data line
        Range("B26").Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=False
        Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=False
        Range("B6").Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=False
        
    End Sub
    
    
    Sub NewTick()
    '
    ' NewTick Macro
    ' Check to see if new Tick data has arrived by comparing current & grabbed VALUES
            ' (Note, not compare times as for 1Min data because the "Time" format does not include seconds
    ' If it is NEW then UPDATE THE TABLE and also paste the new data in the "grabbed" cells
    ' Range E13 is the VALUE of the latestdata available
        If Range("E13") <> Range("E6") Then
        Range("G9").Value = "New"
        Else: Range("G9").Value = ""
        End If
    
    ' If data is NOT new then end this routine
        If Range("G9") = "" Then End
        
    ' If the data IS new then update table:
            ' Shift Tick data from the Second (table) Row up to the First Row
            ' Copy/Paste grabbed Tick data to the last Row
        
    ' Range E18-G26 will need to change: E18:G517 (for 500 data lines)
        Range("E19:G26").Select
        Range("E26").Activate
        Selection.Cut
        Range("E18").Select
        ActiveSheet.Paste
        Range("E13:G13").Select
        Selection.Copy
        Range("E26").Select
        Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=False
        Selection.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:= _
            False, Transpose:=False
        Range("E26:G26").Select
        Application.CutCopyMode = False
        Selection.Copy
        Range("E6").Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
    End Sub
    
    
    Sub DownloadUpdate()
    '
    ' DownloadUpdate Macro
        Application.Run "'Copy of dde - 1.xls'!NewMinClose"
        Application.Run "'Copy of dde - 1.xls'!NewTick"
    End Sub
    
    
    Sub UpdateTable()
    '
    ' Update Table
        Do
        Application.Run "'Copy of dde - 1.xls'!DownloadUpdate"
        Loop
    End Sub
    Last edited by rodrigt; 06-15-2010 at 05:28 AM. Reason: Updating this thread to SOLVED - Thanks!

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Excel Macro DO/LOOP not working after cells update from another application

    You shouldn't delete (cut) the link to where tjhe data come form.
    This suffices
    Avoid activate, Select and copy in VBA
    Sub NewMinClose()
      If Range("D13") <> Range("D6") Then cells(rows.count,20).end(xlup).offset(1).resize(500,5)=Range("B18:G517").value
    End Sub

  3. #3
    Registered User
    Join Date
    06-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2000
    Posts
    13

    Re: Excel Macro DO/LOOP not working after cells update from another application

    Pike wrote:
    > Your first post does not comply with Rule 3 of our Forum RULES.... can you please go back and edit your first post.
    -- My apologies, hope I followed the CODE procedures correctly.

    SNB,
    Thank you for your rapid reply! Being a VBA novice I'll need to digest your suggestions. During the weekend the external data I am attempting to capture into a table does not operate, thus I'll be back early next week.

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Excel Macro DO/LOOP not working after cells update from another application

    Thanks for taking the time to read and comply with the rule .
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  5. #5
    Registered User
    Join Date
    06-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2000
    Posts
    13

    Re: Excel Macro DO/LOOP not working after cells update from another application

    Quote Originally Posted by rodrigt View Post
    Thank you for your rapid reply! Being a VBA novice I'll need to digest your suggestions. During the weekend the external data I am attempting to capture into a table does not operate, thus I'll be back early next week.
    Meanwhile I may have found guidance elsewhere (see below) to solve the problem, but as a macro novice I'm stumbling in the dark. The found guidance entails placing Macros in the Workbook (to run in the background) and a corresponding macro in the worksheet.

    While the database I seek to continuously download from is down during the weekend my "manual workaround" seems to be working, BUT....

    Help Please: How to control (e.g. Stop) a Macro that continuously runs in the background? I've attempted checking a "control cell" where I could enter "Stop" but that didn't really work - the background macro is still in effect. (What I've been doing to interrupt the macro is to invoke it in Editor mode and make a bogus entry in order to cause an error.) Nonetheless as my new Macro seems to be working manually, I would appreciate guidance on how to domesticate a macro that continuously runs in the background.

    Thanks guys,

    Tom

    PS - Here is the guidance I found elsewhere:
    Excel VBA Macro: How to Run a Macro at a Set Time (or continuously)...
    http://www DOT ozgrid DOT com/Excel/run-macro-on-time.htm

    The pertinent section for continuously running in the background (to execute code if the selected parameter changes) is:

    Let's now suppose you want to run this macro (MyMacro) at 15 minute intervals after opening your Workbook. Again we will kick it off as soon as the Workbook Opens so right click on the Excel icon next to "File" and select "View Code". In here put;

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
         Application.OnTime dTime, "MyMacro", , False
    End Sub
    
    Private Sub Workbook_Open()
      Application.OnTime Now + TimeValue("00:15:00"), "MyMacro"
    End Sub
    Now in any Standard Module (Insert>Module) place this;

    Public dTime As Date
    Sub MyMacro()
    dTime = Now + TimeValue("00:15:00")
    Application.OnTime dTime, "MyMacro"
    
    'YOUR CODE
    End Sub

  6. #6
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Excel Macro DO/LOOP not working after cells update from another application

    Hi rodrigt,
    Thanks for sharing and posting the solution.

  7. #7
    Registered User
    Join Date
    06-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2000
    Posts
    13

    Re: Excel Macro DO/LOOP not working after cells update from another application

    Not quite solved yet - How can I stop the continuously running macro? Last night I was able to stop it by entering bogus code into the macro, but somehow that is not working today... And if I shut down and re-start the worksheet the macro auto starts. Note - Per prior dialog (above), this macro "...entails placing Macros in the Workbook (to run in the background) and a corresponding macro in the worksheet."

    I've tried Ctrl-Break and F8 but the macro keeps running and thus I can't debug my code. This problem gets worse when the Macro goes into "compile" and the error windows pop-up continuously. Any suggestions? Thanks.

    PS - I'm also having trouble using If-Then-Else with End in one of the conditional statements, but that would be off-topic for this thread. Once I search this forum I may need to start a new thread.
    Last edited by rodrigt; 06-13-2010 at 07:08 PM. Reason: Wanted to add what else I've tried

  8. #8
    Registered User
    Join Date
    06-11-2010
    Location
    USA
    MS-Off Ver
    Excel 2000
    Posts
    13

    Smile Re: Excel Macro DO/LOOP not working after cells update from another application

    The important thing for other users is that the above mentioned link for running a macro continuously really works! (As a novice I'm still figuring how to deal with it for stopping for editing... but it really works!)

    Next I'll set this thread to "closed" - Thanks Pike!

    Tom

+ 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