Getting data from a closed workbook

muhleebbin

Active Member
Joined
Sep 30, 2017
Messages
252
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2013
  5. 2010
Platform
  1. Windows
  2. MacOS
  3. Mobile
  4. Web
Hi Everyone,

I have the following code however am running into an error...

Capture.JPG


Is it because the file name in fn is from a sharepoint site? When running through the code, it errors out on the rs.open step so it seems like the connection to the file is going fine.

Thanks in advance for your help!

VBA Code:
Sub Getdata()

    Dim cn As ADODB.Connection
    Dim fn As String
    Dim rs As ADODB.Recordset
    
    fn = Sheets("Map").Range("BD14").Value
    
    Set cn = New ADODB.Connection
    
    cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=fn;Extended Properties='Excel 12.0 Xml;HDR=YES';"
    
    cn.Open
    
    Set rs = New ADODB.Recordset
    
    rs.ActiveConnection = cn
    rs.Source = "SELECT * FROM [Sheet31$]"
    rs.Open
    
    Sheet3.Range("A1").CopyFromRecordset rs
    
    rs.Close
    cn.Close

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,223,234
Messages
6,170,891
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