+ Reply to Thread
Results 1 to 11 of 11

Help in macro code to stop code running for certain time

  1. #1
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Question Help in macro code to stop code running for certain time

    Hi all,

    I have myself a macro code does various things but in the middle of it it tells a microsoft query table to refresh itself. It then brings up a userform afterwards. The problem I am having is that bringing up the userform is stopping the query table from refreshing (it won't refresh until I manually close the userform).

    I have tried the application.wait function but this doesn't actually stop the code - it just pauses it for x seconds (my mouse pointer goes into the 'waiting' mode) so the table still doesn't refresh itself.

    What I am after basically is somehow that I can stop the code completely and then start it again 2 or 3 seconds afterwards?

    Can anyone help?

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Help in macro code to stop code running for certain time

    When you command the query table to refresh, set the BackgroundQuery argument to False. This will wait for the query table to complete the refresh before loading your userform.

    MyQuerryTable
    .Refresh(BackgroundQuery:=False)
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Help in macro code to stop code running for certain time

    OK thanks - I've copied that in exactly and replace the italics with my table name and it comes up with a syntax error. Does it need ""?

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Help in macro code to stop code running for certain time

    Quote Originally Posted by ExcelFailure View Post
    OK thanks - I've copied that in exactly and replace the italics with my table name and it comes up with a syntax error. Does it need ""?
    Remove the parenthesis

    MyQuerryTable.Refresh BackgroundQuery:=False

  5. #5
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Help in macro code to stop code running for certain time

    Many thanks I will give that a go tomorrow

  6. #6
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Help in macro code to stop code running for certain time

    Hi, it now comes up with a runtime error 424 'object required' and highlights your bit of code when I debug it.

    Here is my code in full:

    Please Login or Register  to view this content.

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Help in macro code to stop code running for certain time

    Is Data defined somewhere? If yes, show the code. If no, that's the missing object. You have to define the object Data.

  8. #8
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Help in macro code to stop code running for certain time

    OK Data is the query table - I have named it Data

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Help in macro code to stop code running for certain time

    You cannot reference it by Name like that. Try this syntax. Change the sheet name to suit.

    Sheets("Sheet1").QueryTables("Data").Refresh BackgroundQuery:=False

  10. #10
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Help in macro code to stop code running for certain time

    I've tried that and it comes up with a runtime error 9 'subscript out of range'?

  11. #11
    Forum Contributor
    Join Date
    03-06-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2010
    Posts
    352

    Re: Help in macro code to stop code running for certain time

    Anyone got any thoughts on this?

    Just to recap, the code is:

    Please Login or Register  to view this content.
    It comes up with a runtime error 9 'subscript out of range'

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. If Statement to stop Dim code from running
    By rlh in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-30-2014, 09:30 AM
  2. [SOLVED] How to stop a code from running using an another code/macro
    By danjim02 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-06-2013, 08:21 AM
  3. Stop code before running next part
    By Sniper in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-01-2009, 01:48 AM
  4. Code to stop a worksheet_selectionchange macro running
    By hriggs in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-17-2009, 06:23 AM
  5. [SOLVED] stop running code
    By lou sanderson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-29-2005, 05:25 PM

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