Power Query - Data Refresh Error in Macro

jaihawk8

Board Regular
Joined
Mar 23, 2018
Messages
69
Office Version
  1. 2016
Platform
  1. Windows
I have created a file in PowerQuery that will pull in the Earnings Statement for an Employee. I have the file pulling in the Sales Rep name and the Payroll Date from another file and that part is working just fine. I am now trying to get the file to Refresh the PowerQuery. When I do it manually, it works just fine, but when I try to do it as part of this macro, I get a message saying "This will cancel a pending data refresh. Continue?".

What am I doing wrong?

VBA Code:
Sub UpdateFilesInPath()
    Dim folderPath As String
    Dim fileName As String
    Dim originalValue As Variant
    Dim newWB As Workbook


    ' Get the folder path from cell B19
    folderPath = ThisWorkbook.Sheets("Main").Range("B19").Value
    
    'Retrieve the Payroll Date from cell B3 of the original workbook
        originalValue = ThisWorkbook.Sheets("Main").Range("B3").Value

    ' Check if the folder path exists
    If Dir(folderPath, vbDirectory) = "" Then
        MsgBox "Folder path does not exist.", vbExclamation
        Exit Sub
    End If

    ' Disable screen updating to speed up the process
    ' Application.ScreenUpdating = False

    ' Loop through all files in the folder
    fileName = Dir(folderPath & "\*.xls*")
    Do While fileName <> ""
        ' Open the current file
        Workbooks.Open folderPath & fileName, IgnoreReadOnlyRecommended:=True, UpdateLinks:=0
        
        ' Set active workbook
        Set newWB = ActiveWorkbook
        
        'Set the value of cell C2 in opened workbook
        newWB.Sheets("Hidden").Range("C2").Value = originalValue

        ' Update the workbook (replace this line with your update code)
        ActiveWorkbook.RefreshAll

        ' Save and close the current file
        newWB.Save
        newWB.Close

        ' Move to the next file
        fileName = Dir()
    Loop

    ' Re-enable screen updating
    ' Application.ScreenUpdating = True

    MsgBox "Files updated successfully.", vbInformation
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
At which code line do you get this message?
I assume the queries are refreshed in the background so the code doesn't wait for them to finish.
Disable the queries background refresh, so the code will wait for them before trying to save and close the file.
Check this link for code to automate this.
 
Upvote 0
Have you checked background refresh
- Go to the data tab, click connections in the connections group, click on ALL the connections and click properties, un-check the box that says "Enable background refresh"
1712216773950.png
 
Upvote 0
Go to the data tab, click connections in the connections group, click on ALL the connections and click properties, un-check the box that says "Enable background refresh"
The problem is that the code suggests that it opens all files in a folder and does something ...
fileName = Dir(folderPath & "\*.xls*")
Do While fileName <> ""
... so with many potentially changing files the menu is quite a tedious option :)
 
Upvote 0

Forum statistics

Threads
1,223,871
Messages
6,175,099
Members
452,612
Latest member
MESTeacher

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top