filmov
tv
Wise Owl Answers - How do I get data from multiple workbooks with one query in VBA?
![preview_player](https://i.ytimg.com/vi/J07ztF26yLc/maxresdefault.jpg)
Показать описание
One way to get data from closed Excel workbooks is to open each file before extracting the information, but did you know that in VBA you can connect to a workbook and extract data from it without needing to open the file? Even better, if the files you're connecting to have the same structure, you can write a single SQL SELECT statement to select the contents of all the files in one go! This video explains how to do this using the ActiveX Data Objects library. The video shows how to loop through a collection of Excel files in a folder. You'll learn how to create a connection object and build a SQL UNION ALL query to get the data from each closed Excel file. You'll also see how to create a recordset object and load a set of data into it using the SQL statement that you've constructed. 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 closed workbooks.
Chapters
00:00 The Question
01:22 Connecting to One Workbook
06:38 Populating a Recordset
08:37 Writing Data to the Worksheet
10:29 The UNION and UNION ALL Keywords
11:48 Selecting from Multiple Files with One Connection
14:02 Finding an Excel File in a Folder
17:16 Looping Through Files in a Folder
19:18 Building a Select Statement in a Loop
23:24 Returning the Results to the Worksheet
23:56 Sorting the Query Results
25:04 Adding a Where Clause to Filter the Results
26:30 Adding Multiple Criteria
Комментарии