+ Reply to Thread
Results 1 to 3 of 3

Loop not waiting for calculation before proceeding

  1. #1
    Registered User
    Join Date
    02-24-2018
    Location
    Earth
    MS-Off Ver
    Office Pro Plus 2013
    Posts
    6

    Loop not waiting for calculation before proceeding

    I have a loop with 3 web queries that are brought into a renamed copy of a template worksheet. As this is part of a larger workbook I have the calculations set to manual. A block of formulas on the renamed worksheet are suppose to update. The block is then copied and the values pasted onto another worksheet. The renamed worksheet is then deleted and the whole process starts again. Everything works fine except that the formulas do not complete their update before they are copied and their values pasted when the loop is run. The calculations work perfectly when the loop is stepped though using F8.

    I have tried various methods (wait, sleep, Application.CalculateUntilAsyncQueriesDone, DoEvents, turning the calculations to automatic, etc.) to allow time for the calculations to complete before the copy step is executed. There is nothing overly complex about the formulas (counts, some if statements, date calculations, and lookups). The formals update in less than a second when the loop is stepped though using F8.



    A short snippet of the code is as follows:

    Application.Calculation = xlManual
    j = 1
    Set MyRange = Sheets("Source").Range("D11")
    Set MyRange = Range(MyRange, MyRange.End(xlDown))

    For Each MyCell In MyRange

    Sheets("Template").Select
    Sheets("Template").Copy After:=Sheets(Sheets.Count)
    Sheets(Sheets.Count).Name = MyCell.Value

    ActiveSheet.Calculate
    Sheets(MyCell.Value).Range("O2").QueryTable.Refresh BackgroundQuery = False
    Sleep 200
    Sheets(MyCell.Value).Range("AH2").QueryTable.Refresh BackgroundQuery = False
    Sleep 200
    Sheets(MyCell.Value).Range("AV2").QueryTable.Refresh BackgroundQuery = False
    Sleep 200
    ActiveSheet.Calculate

    Sheets(MyCell.Value).Range("B3:L58").Copy
    Sheets("Data").Select
    Range("A3").Offset(0, (j - 1) * 11).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

    Application.DisplayAlerts = False
    Worksheets(MyCell.Value).Delete
    Application.DisplayAlerts = True

    j = j + 1

    Next MyCell



    Any help is greatly appreciated.

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

    Re: Loop not waiting for calculation before proceeding

    I suspect the queries don't complete before the rest of the code tries to copy\paste.

    For each of your queries, try setting .BackgroundQuery = False

    This allows the query to complete before continuing with the rest of the code.
    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
    Registered User
    Join Date
    02-24-2018
    Location
    Earth
    MS-Off Ver
    Office Pro Plus 2013
    Posts
    6

    Re: Loop not waiting for calculation before proceeding

    Maybe I'm missing something, but is what you are suggesting any different from what I already have? Is it just a different syntax?

    As an example: "Sheets(MyCell.Value).Range("O2").QueryTable.Refresh BackgroundQuery = False" appears to me to be functionally the same.

    As a side note, I also made sure that none of the data connections "Enable background refresh" boxes were checked.

+ 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. Waiting for Query to be executed - waiting forever!
    By BHeup in forum For Other Platforms(Mac, Google Docs, Mobile OS etc)
    Replies: 0
    Last Post: 07-01-2018, 11:52 PM
  2. [SOLVED] VBA Solver Loop while waiting to complete
    By richardking in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-16-2017, 10:52 AM
  3. [SOLVED] Calculating Differences between dates for a Waiting List Calculation
    By richiev86 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-14-2014, 12:42 PM
  4. Waiting time calculation
    By mojupe in forum Excel General
    Replies: 2
    Last Post: 07-03-2011, 03:00 AM
  5. Fill Proceeding rows
    By rhua5436 in forum Excel General
    Replies: 3
    Last Post: 07-31-2009, 09:57 PM
  6. Verify before proceeding
    By snowing in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-06-2006, 08:49 AM
  7. [SOLVED] Waiting for the calculation to be completed
    By saita in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-01-2005, 12:05 AM

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