Excel Data Connection errors on Refresh

后端 未结 4 2035
傲寒
傲寒 2021-02-14 09:08

Solved! See below for solution!

I\'m in Excel 2010 connecting to multiple, seperate Access 2010 db\'s from Excel through PivotTable dat

4条回答
  •  迷失自我
    2021-02-14 09:43

    So I had a similar error when I was trying to create a VBA script to refresh an excel workbook automatically at a given time, and there were a few things that I did in my VBA script to get this to work. One of them being disabling background refresh. That could be your issue and you can easily do disable it by going to your connection properties and disabling background refreshing.

    Here is what I did in VBA when I was getting this error, although I will say I was not using it with an MS access db. I had one excel workbook that I used as a 'runner' and it opened up the other books one by one and refreshed their connections. Basically I had a variable for the path and extension and put the names of each workbook into an array and looped through the array.

    I combined the path and extension to give me the full filename of the file, you will see that in the loop.

    This is what my loop looked like :

    For i = LBound(testArray) To UBound(testArray)
        Dim wb As Workbook
        Set wb = Workbooks.Open(path & testArray(i) & ext, 0, False)
    
        'Next I checked to see if the workbook was in protected view and allowed for editing.
        If Application.ProtectedViewWindows.Count > 0 Then
            Application.ActiveProtectedViewWindow.Edit
        End If
    
        'Now comes the part that I believe should help for your case
        wb.Connections(testArray(i) & "This is your connection name").OLEDBConnection.BackgroundQuery = False
        wb.RefreshAll
        wb.Connections(testArray(i) & "This is your connection name").OLEDBConnection.BackgroundQuery = True
    
        wb.SaveAs fileName:= "Thisbook.xlsx"
        wb.Close
    Next i
    

    To get the connection name there are a few ways, including just looking to see what it is manually. For me because I wanted to make it so that I didn't need to manually put in every connection name I used the inherent pattern that I saw with the connections names.

    In my case that was the baseNameOfWorkbook & " POS Report"

    I do believe that you may be getting the errors due to background refreshing. So if you don't need to do this in VBA I would just suggest going to connection properties and disabling it.

    Let me know if this works.

提交回复
热议问题