Forum Discussion

AydeeDiaz's avatar
AydeeDiaz
Copper Contributor
Oct 20, 2022

Wait until query is refreshed to run the next macro

Hi! I am trying to create a macro, but I need to wait until query is refreshed before doing the copy to paste in the workbook

 

Sub OpenAnyFile()

VBA.Shell "Explorer.exe C:\Users\vjrdcx\Aptiv\Aptiv Global Business Services - Documents\SPM\Open Items Report\OTC Semantic 2.0 AS&UX 2.xlsm", vbMaximizedFocus

'Wait until query is refreshed

End Sub

'Private Sub WorkbookCopyData()

'Workbooks("OTC Semantic 2.0 AS&UX 2.xlsm").Worksheets("AS&UX").Range("A2:AZ60000").Copy_
'Workbooks("2022.09.00 AS&UX Open Items.xlsm").Worksheets("AS&UX").Range ("A2")

'End Sub

Sub CopyData()

Dim mWB As Workbook 'This Workbook
'XDim dWB As Workbook 'XXData Workbook

'Set the main workbook
Set mWB = ThisWorkbook

'XX Selection the file manually XX
'XDim filePath As String
'XfilePath = Application.GetOpenFilename
'XDebug.Print filePath

'XX Set the data workbook XX
'XSet dWB = Workbooks.Open(filePath)
'XDIM dataSh As Worksheet
'XSet dataSh = dWB.Sheets("AS&UX")

'XX Copy the data table XX
'XdataSh.Range("A1").CurrentRegion.Copy

Workbooks("OTC Semantic 2.0 AS&UX 2.xlsm").Worksheets("AS&UX").Range("A2:AZ60000").Copy

'Paste the data table
mWB.Sheets("AS&UX").Range("A2").PasteSpecial xlPasteAll

'XsWB.Close False


End Sub

Sub WorkbookChange()

'ThisWorkbook.RefreshAll

'For Each Workbook In ThisWorkbook.Sheets
'For Each pvt In Workbook.PivotTables
'pvt.RefreshTable
'pvt.Update
'Next
'Next

'PivotTables("PivotTable10").RefreshTable

Dim pt As PivotTable

For Each pt In ThisWorkbook.Worksheets("PIVOT").PivotTables

pt.PivotCache.Refresh

Next pt

'Worksheets("PIVOT").PivotTables("PivotTable10").PivotCache.Refresh

End Sub

'Close Workbook datasource

Sub CloseWorkbook()

Workbooks("OTC Semantic 2.0 AS&UX 2.xlsm").Close SaveChanges = True

End Sub

'Completed message

Sub Message()

MsgBox ("The Refreshing is Completed!")

End Sub

'Save Workbook with today;s date

Sub SaveToday()

ActiveWorkbook.SaveAs ("C:\Users\vjrdcx\Aptiv\Aptiv Global Business Services - Documents\SPM\Open Items Report") & ".xlsx"

End Sub

 

7 Replies

      • JKPieterse's avatar
        JKPieterse
        Silver Contributor
        There is a checkbox in the query properties that either allows or disallows the query to run in the background. Make sure it disallows it.

Resources