Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
551 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Posted by Andrew Gould on 08 April 2021
One way to get data from a closed Excel workbook is to open it first, but did you know that in VBA you can connect to a workbook and extract data from it without needing to open the file? This video explains how to do this using the ActiveX Data Objects library. You'll learn how to create a connection object and construct a connection string to talk to the closed Excel file. You'll also see how to create a recordset object and load a set of data into it using an SQL Select statement. As a bonus, the video shows you how to add some basic criteria to the SQL query to control which rows of data you return from the workbook.
See our full range of VBA training resources, or test your knowledge of VBA with one of our VBA skills assessment tests.
This video has the following accompanying files:
File name | Type | Description |
---|---|---|
Get Data from Closed Workbook.xlsm | Excel workbook with macros | |
Movies.xlsx | Excel workbook |
Click to download a zipped copy of the above files.
There are no exercises for this video.
You can increase the size of your video to make it fill the screen like this:
Play your video (the icons shown won't appear until you do), then click on the full screen icon which appears as shown above.
When you've finished viewing a video in full screen mode, just press the Esc key to return to normal view.
To improve the quality of a video, first click on the Settings icon:
Make sure yoiu're playing your video so that the icons shown above appear, then click on this gear icon.
Choose to change the video quality:
Click as above to change your video quality.
The higher the number you choose, the better will be your video quality (but the slower the connection speed):
Don't choose the HD option shown unless your connection speed is fast enough to support it!
Is your Wise Owl speaking too slowly (or too quickly)? You can also use the Settings menu above to change your playback speed.
From: | CrickPick |
When: | 09 Nov 23 at 19:22 |
Hi there,
My coworker and I were running into an error, Run-time error '-2147217865 (80040e37)': Automation error, when we hit the rs.open line. We both tried posting the code in the comments of the YouTube video, but they never seemed to stick. Below is the full code we are using.
Thanks so much in advance for any help with this!
Christina
Dim MyFilesPath As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Sheet1.Range("A1").CurrentRegion.Offset(1, 0).Clear
MyFilesPath = "G:\TR\DATA_TEAM_REPORTS&QC\PerryC"
Set cn = New ADODB.Connection
cn.ConnectionString = _
"Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & MyFilesPath & "DupeIMBTest - CP Edit.xlsx;" & _
"Extended Properties='Excel 12.0 Xml;HDR=YES';"
cn.Open
Set rs = New ADODB.Recordset
rs.ActiveConnection = cn
rs.Source = _
"SELECT * FROM [Sheet1$] " ' _
' & "WHERE [Acronym] = 'NWW' AND [Job] LIKE 'Weekly%'"
rs.Open
Sheet1.Range("A2").CopyFromRecordset rs
Sheet1.Range("A1").CurrentRegion.EntireColumn.AutoFit
rs.Close
cn.Close
From: | Andrew G |
When: | 10 Nov 23 at 07:17 |
Hi Christina,
Happy to see that you got your code posted!
At first glance it looks as though you might be missing a \ at the end of your folder path. Try this and see if it improves things:
MyFilesPath = "G:\TR\DATA_TEAM_REPORTS&QC\PerryC\"
I'm assuming that PerryC is a folder and not part of the file name?
Let me know if it helps!
From: | SIH007 |
When: | 24 Apr 21 at 16:06 |
How do I get data from a closed Excel file using VBA?
Hi Andrew,
I really love your videos they always have so much information in it!
I just finished watching a. m. video. My problem is the following: I have to grab data from a closed Excel-file, but the data table I need unfortunately is not positioned in row 1 - there has to remain some descriptive stuff above the very table, so I could of course grab the entire table and delete all rows which don*t belong to the datatable.
But actually I would only need a very small portion of the data, here I would only need column 3 and 4 of the datatable and only values which are bigger than a number on another sheet. I work a lot with SQL statements so it would not be a problem at all to create the statement I need, but I can't address the column names, because they are not in the first row (I would not even need the headers)
Is there a possibility to address the data using a named range instead? Or do you have another idea how to solve that problem?
Best regards,
Susanne
From: | Andrew G |
When: | 26 Apr 21 at 08:05 |
Hi Susanne,
Yes, you can use range names in your select statement rather than sheet names. If your range name refers to the entire data table without column headers make sure that you set the HDR extended property to NO in your connection string:
HDR=NO
You can then use generic placeholder field names, so if you only need columns 3 and 4 you can write something like this:
"SELECT F3, F4 FROM [MyRangeName]"
I hope that helps!
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.