Oct 20 2022 04:22 PM
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
Oct 21 2022 05:21 AM
Oct 24 2022 10:02 AM
Sorry what do you mean??
Oct 24 2022 10:40 AM
Oct 24 2022 10:44 AM
Oct 25 2022 12:50 AM - edited Oct 25 2022 12:51 AM
As long as the queries are set up to be synchronous, the VBA code does not matter as it will wait for the refresh to finish before proceeding.
Oct 25 2022 10:48 AM
Oct 26 2022 01:01 AM
@AydeeDiaz Try 2 does not look wrong to me, if you change ThisWorkbook to ActiveWorkbook it might just work?