I am trying to use a macro to refresh data from another excel file but I keep getting an error. Any ideas why?

oct2006

New Member
Joined
Jun 8, 2018
Messages
1
Hello,

I have 6 Excels files. I am using one file as the main file, and the other five I am using as data connections for the main file. My code is as follows

Sub DataRefresh()

Dim wkb as Workbook
Set wkb = Workbooks.Open("filepath")
wkb.RefreshAll (This is where the debugger says the error is)
wkb.Close SaveChanges:=True

*Repeat with different names for other files*

ThisWorkbook.RefreshAll

End Sub

Any ideas?


EDIT
Here's the full code:

Code:
Sub DataRefreshTest()
         
    'Declaration - declare an object for setting the workbook
    
    Dim wkb As Workbook
    Dim wkb1 As Workbook
    Dim wkb2 As Workbook
    Dim wkb3 As Workbook
    Dim wkb4 As Workbook
    
    
    'Open Workbook and set an object(wkb)
        
    Set wkb1 = Workbooks.Open("C:\Users\nb954s\Documents\Exchange_Rate_Data_A_C")
    wkb1.RefreshAll
    wkb1.Close SaveChanges:=True
    
    Set wkb2 = Workbooks.Open("C:\Users\nb954s\Documents\Exchange_Rate_Data_D_K")
    wkb2.RefreshAll
    wkb2.Close SaveChanges:=True
    
    Set wkb3 = Workbooks.Open("C:\Users\nb954s\Documents\Exchange_Rate_Data_L_R")
    wkb3.RefreshAll
    wkb3.Close SaveChanges:=True
    
    Set wkb4 = Workbooks.Open("C:\Users\nb954s\Documents\Exchange_Rate_Data_S_Z")
    wkb4.RefreshAll
    wkb4.Close SaveChanges:=True
    
    Set wkb = Workbooks.Open("C:\Users\nb954s\Documents\Exchange_Rate_Summary")
    wkb.RefreshAll
    wkb.Close SaveChanges:=True
    
    ThisWorkbook.RefreshAll
    
    MsgBox "Data Refresh complete. All values have been updated. Click 'OK' to continue"

End Sub
 
Last edited by a moderator:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
I set up this code and changed the files to point to valid files on my computer and I do not get any error. I am guessing that it is an issue within your files specifically. Are you getting an error message? You said you keep getting an error, can you provide some details on specifically what the error is or what is happening. Is there a certain line of code causing the problem? Need more info.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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