Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4
Default Wait statement in macro

I have a macro that runs a query against an external data source and then
updates the range of a pivot table based on the number of rows and columns
the query retutrns. When I run the macro in debug mode everything works
fine. When I run it from the command button the range is not updated. I
suspect that since the query is running in background mode, that the range
calculations happen before the results are returned. What I need to be able
to do is have the macro wait until the query results are returned before
continuing on. Is there a way to do this? Here is the macro:

Option Explicit

Sub Kintana_Update()
'
' Kintana_Update Macro
' Macro recorded 02/05/2010
' This macro runs the data query to Relay and updates
' the range on the Service Level Work pivot table with the
' number of rows returned from the query

Dim wks As Worksheet
Dim LastRow As Long
Dim LastCol As Long
'
' Query Relay
Sheets("Relay Data").Select
Range("A2").Select
Selection.QueryTable.Refresh BackgroundQuery:=True

' Calculate the range of data returned from the query
With ActiveSheet
LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

' Refresh the main pivot table with the new range of data
Sheets("PF Kintanas Pivot Tables").Select
Range("D1").Select
ActiveSheet.PivotTables("Service Level Work").PivotSelect "",
xlDataAndLabel, _
True
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
"'Relay Data'!R2C1:R" & LastRow & "C" & LastCol
Sheets("PF Kintanas Pivot Tables").PivotTables("Service Level
Work").PivotCache.Refresh
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,939
Default Wait statement in macro

Try changing the BackgroundQuery to false
--
HTH...

Jim Thomlinson


"tjfwestcoast" wrote:

I have a macro that runs a query against an external data source and then
updates the range of a pivot table based on the number of rows and columns
the query retutrns. When I run the macro in debug mode everything works
fine. When I run it from the command button the range is not updated. I
suspect that since the query is running in background mode, that the range
calculations happen before the results are returned. What I need to be able
to do is have the macro wait until the query results are returned before
continuing on. Is there a way to do this? Here is the macro:

Option Explicit

Sub Kintana_Update()
'
' Kintana_Update Macro
' Macro recorded 02/05/2010
' This macro runs the data query to Relay and updates
' the range on the Service Level Work pivot table with the
' number of rows returned from the query

Dim wks As Worksheet
Dim LastRow As Long
Dim LastCol As Long
'
' Query Relay
Sheets("Relay Data").Select
Range("A2").Select
Selection.QueryTable.Refresh BackgroundQuery:=True

' Calculate the range of data returned from the query
With ActiveSheet
LastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
End With

' Refresh the main pivot table with the new range of data
Sheets("PF Kintanas Pivot Tables").Select
Range("D1").Select
ActiveSheet.PivotTables("Service Level Work").PivotSelect "",
xlDataAndLabel, _
True
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
"'Relay Data'!R2C1:R" & LastRow & "C" & LastCol
Sheets("PF Kintanas Pivot Tables").PivotTables("Service Level
Work").PivotCache.Refresh
End Sub

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
sendkeys(keys,wait) how do I use wait MM Excel Discussion (Misc queries) 1 February 11th 09 03:47 PM
Macro wait 30 seconds then Complete the Macro Vick Excel Discussion (Misc queries) 2 June 2nd 08 08:04 PM
Wait function in Excel 2000 Macro fryb53 Excel Discussion (Misc queries) 3 January 16th 08 02:49 PM
Should I Wait? Chaplain Doug Excel Discussion (Misc queries) 0 February 6th 07 04:58 PM
How do I get an excel macro to stop at a cell & wait for imput? oscar_az Excel Discussion (Misc queries) 1 January 11th 06 01:38 PM


All times are GMT +1. The time now is 09:26 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"